- 浏览: 5770230 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (890)
- WindowsPhone (0)
- android (88)
- android快速迭代 (17)
- android基础 (34)
- android进阶 (172)
- android高级 (0)
- android拾遗 (85)
- android动画&效果 (68)
- Material Design (13)
- LUA (5)
- j2me (32)
- jQuery (39)
- spring (26)
- hibernate (20)
- struts (26)
- tomcat (9)
- javascript+css+html (62)
- jsp+servlet+javabean (14)
- java (37)
- velocity+FCKeditor (13)
- linux+批处理 (9)
- mysql (19)
- MyEclipse (9)
- ajax (7)
- wap (8)
- j2ee+apache (24)
- 其他 (13)
- phonegap (35)
最新评论
-
Memories_NC:
本地lua脚本终于执行成功了,虽然不是通过redis
java中调用lua脚本语言1 -
ZHOU452840622:
大神://处理返回的接收状态 这个好像没有监听到 遇 ...
android 发送短信的两种方式 -
PXY:
拦截部分地址,怎么写的for(int i=0;i<lis ...
判断是否登录的拦截器SessionFilter -
maotou1988:
Android控件之带清空按钮(功能)的AutoComplet ...
自定义AutoCompleteTextView -
yangmaolinpl:
希望有表例子更好。。。,不过也看明白了。
浅谈onInterceptTouchEvent、onTouchEvent与onTouch
这个控件本人强烈推荐,它会使得布局非常的简单且高效;
下面这个布局如果是你,你会用多少层?多少控件生成?
告诉你吧,一个SpannableTextView控件就搞定了!
它把TextView和Spannable封装在了一起,可以在一个TextView中显示不同的字体颜色,大小,背景色等;
它支持如下样式:
* Babushka Method Internal Span
* textSize AbsoluteSizeSpan
* textColor ForegroundColorSpan
* textSizeRelative RelativeSizeSpan
* backgroundColor BackgroundColorSpan
* style StyleSpan
* underline UnderlineSpan
* strike StrikethroughSpan
* superscript SuperscriptSpan
* subscript SubscriptSpan
用法也很简单:
源码:
试着结合这个类Phrase.java那就更爽了;
ColorPhrase实现处理带颜色的字符串
https://github.com/THEONE10211024/ColorPhrase
https://github.com/quiqueqs/BabushkaText
Spanny实现字符串样式处理
https://github.com/binaryfork/Spanny
下面这个布局如果是你,你会用多少层?多少控件生成?
告诉你吧,一个SpannableTextView控件就搞定了!
它把TextView和Spannable封装在了一起,可以在一个TextView中显示不同的字体颜色,大小,背景色等;
它支持如下样式:
* Babushka Method Internal Span
* textSize AbsoluteSizeSpan
* textColor ForegroundColorSpan
* textSizeRelative RelativeSizeSpan
* backgroundColor BackgroundColorSpan
* style StyleSpan
* underline UnderlineSpan
* strike StrikethroughSpan
* superscript SuperscriptSpan
* subscript SubscriptSpan
用法也很简单:
/** * 为一个TextView设置多种字体(大小,颜色,背景色等) * * @param tv * @param title * @param content */ public void createSpannableTextView(SpannableTextView tv, String title, String content) { // clear pieces tv.reset(); // Add the first piece tv.addPiece(new SpannableTextView.Piece.Builder(title).textColor(App.res.getColor(R.color.text_color_c2)) .textSize((int) App.res.getDimension(R.dimen.font_xbig)).build()); // Add the second piece tv.addPiece(new SpannableTextView.Piece.Builder(content).textColor(App.res.getColor(R.color.text_color_c8)) .textSize((int) App.res.getDimension(R.dimen.font_middle)).build()); // Display the final, styled text tv.display(); }
SpannableTextView tv = null; tv = (SpannableTextView) (v.findViewById(R.id.spannableTextView0)); context.createSpannableTextView(tv, "血糖\n", "记录血糖指数"); tv = (SpannableTextView) (v.findViewById(R.id.spannableTextView1)); context.createSpannableTextView(tv, "血压\n", "记录血压指数"); tv = (SpannableTextView) (v.findViewById(R.id.spannableTextView2)); context.createSpannableTextView(tv, "体重\n", "记录体重"); tv = (SpannableTextView) (v.findViewById(R.id.spannableTextView3)); context.createSpannableTextView(tv, "饮食\n", "记录日常饮食"); tv = (SpannableTextView) (v.findViewById(R.id.spannableTextView4)); context.createSpannableTextView(tv, "运动\n", "记录运动时间");
<cn.tangdada.tangbang.widget.SpannableTextView android:id="@+id/spannableTextView0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:background="@drawable/line_bottom" android:drawableRight="@drawable/arrow_right" android:drawableLeft="@drawable/icon_0" android:gravity="center_vertical" android:lineSpacingExtra="4dp" android:paddingRight="16dp" android:singleLine="false" />
源码:
package cn.tangdada.tangbang.widget; /* * Copyright (C) 2014 Henrique Boregio. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @author Henrique Boregio (hboregio@gmail.com) */ import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; import android.text.Spannable; import android.text.SpannableString; import android.text.style.AbsoluteSizeSpan; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.SubscriptSpan; import android.text.style.SuperscriptSpan; import android.text.style.UnderlineSpan; import android.util.AttributeSet; import android.widget.TextView; /** * usage: * * <pre> * SpannableTextView tv = (SpannableTextView) findViewById(R.id.spannable_textview); * * // Add the first piece "Central Park" * tv.addPiece(new SpannableTextView.Piece.Builder("Central Park, NY\n").textColor(Color.parseColor("#414141")).build()); * * // Add the second piece "1.2 mi" * tv.addPiece(new SpannableTextView.Piece.Builder("1.2 mi ").textColor(Color.parseColor("#0081E2")).textSizeRelative(0.9f).build()); * * // Add the third piece "from here" * tv.addPiece(new SpannableTextView.Piece.Builder("from here").textColor(Color.parseColor("#969696")).textSizeRelative(0.9f).build()); * * // Display the final, styled text * tv.display(); * </pre> * * <pre> * // grab the Piece at position 1 * Piece piece = babushka.getPiece(1); * * // modify it's text * piece.setText("1.9 km "); * * // you must always call display after you alter a Piece's text * tv.display(); * </pre> * * <pre> * Babushka Method Internal Span * textSize AbsoluteSizeSpan * textColor ForegroundColorSpan * textSizeRelative RelativeSizeSpan * backgroundColor BackgroundColorSpan * style StyleSpan * underline UnderlineSpan * strike StrikethroughSpan * superscript SuperscriptSpan * subscript SubscriptSpan * </pre> * * BabushkaText is a TextView which lets you customize the styling of parts of your text via Spannables, but without the * hassle of having to deal directly with Spannable themselves. * * The idea behind a BabushkaText is that it is made up of {@code Piece}s. Each Piece represents a section of the final * text displayed by this TextView, and each Piece may be styled independently from the other Pieces. When you put it * all together, the final results is still a a single TextView, but with a a very different graphic output. * * * https://github.com/quiqueqs/BabushkaText */ public class SpannableTextView extends TextView { // some default params private static int DEFAULT_ABSOLUTE_TEXT_SIZE; private static float DEFAULT_RELATIVE_TEXT_SIZE = 1; private List<Piece> mPieces; /** * Create a new instance of a this class * * @param context */ public SpannableTextView(Context context) { super(context); init(); } public SpannableTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SpannableTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPieces = new ArrayList<Piece>(); SpannableTextView.DEFAULT_ABSOLUTE_TEXT_SIZE = (int) getTextSize(); } /** * Use this method to add a {@link SpannableTextView.BabushkaText.Piece} to a BabushkaText. Each * {@link SpannableTextView.BabushkaText.Piece } is added sequentially, so the order you call this method matters. * * @param aPiece the Piece */ public void addPiece(Piece aPiece) { mPieces.add(aPiece); } /** * Adds a Piece at this specific location. The underlying data structure is a {@link java.util.List}, so expect the * same type of behaviour. * * @param aPiece the Piece to add. * @param location the index at which to add. */ public void addPiece(Piece aPiece, int location) { mPieces.add(location, aPiece); } /** * Replaces the Piece at the specified location with this new Piece. The underlying data structure is a * {@link java.util.List}, so expect the same type of behaviour. * * @param newPiece the Piece to insert. * @param location the index at which to insert. */ public void replacePieceAt(int location, Piece newPiece) { mPieces.set(location, newPiece); } /** * Removes the Piece at this specified location. The underlying data structure is a {@link java.util.List}, so * expect the same type of behaviour. * * @param location the index of the Piece to remove */ public void removePiece(int location) { mPieces.remove(location); } /** * Clear all the Pieces, same as reset() */ public void clearPiece() { mPieces.clear(); } /** * Get a specific {@link SpannableTextView.BabushkaText.Piece} in position index. * * @param location position of Piece (0 based) * @return Piece o null if invalid index */ public Piece getPiece(int location) { if (location >= 0 && location < mPieces.size()) { return mPieces.get(location); } return null; } /** * Call this method when you're done adding {@link SpannableTextView.BabushkaText.Piece}s and want this TextView to * display the final, styled version of it's String contents. * * You MUST also call this method whenever you make a modification to the text of a Piece that has already been * displayed. */ public void display() { // generate the final string based on the pieces StringBuilder builder = new StringBuilder(); for (Piece aPiece : mPieces) { builder.append(aPiece.text); } // apply spans int cursor = 0; SpannableString finalString = new SpannableString(builder.toString()); for (Piece aPiece : mPieces) { applySpannablesTo(aPiece, finalString, cursor, cursor + aPiece.text.length()); cursor += aPiece.text.length(); } // set the styled text setText(finalString); } private void applySpannablesTo(Piece aPiece, SpannableString finalString, int start, int end) { if (aPiece.subscript) { finalString.setSpan(new SubscriptSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (aPiece.superscript) { finalString.setSpan(new SuperscriptSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (aPiece.strike) { finalString.setSpan(new StrikethroughSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (aPiece.underline) { finalString.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } // style finalString.setSpan(new StyleSpan(aPiece.style), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // absolute text size finalString.setSpan(new AbsoluteSizeSpan(aPiece.textSize), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // relative text size finalString.setSpan(new RelativeSizeSpan(aPiece.textSizeRelative), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // text color finalString.setSpan(new ForegroundColorSpan(aPiece.textColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // background color if (aPiece.backgroundColor != -1) { finalString.setSpan(new BackgroundColorSpan(aPiece.backgroundColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } /** * Resets the styling of this view and sets it's content to an empty String. */ public void reset() { mPieces = new ArrayList<Piece>(); setText(""); } /** * Change text color of all pieces of textview. */ public void changeTextColor(int textColor) { for (Piece mPiece : mPieces) { mPiece.setTextColor(textColor); } display(); } /** * A Piece represents a part of the text that you want to style. Say for example you want this BabushkaText to * display "Hello World" such that "Hello" is displayed in Bold and "World" is displayed in Italics. Since these * have different styles, they are both separate Pieces. * * You create a Piece by using it's {@link SpannableTextView.BabushkaText.Piece.Builder} * */ public static class Piece { private String text; private int textColor; private final int textSize; private final int backgroundColor; private final float textSizeRelative; private final int style; private final boolean underline; private final boolean superscript; private final boolean strike; private final boolean subscript; public Piece(Builder builder) { this.text = builder.text; this.textSize = builder.textSize; this.textColor = builder.textColor; this.backgroundColor = builder.backgroundColor; this.textSizeRelative = builder.textSizeRelative; this.style = builder.style; this.underline = builder.underline; this.superscript = builder.superscript; this.subscript = builder.subscript; this.strike = builder.strike; } /** * Sets the text of this Piece. If you're creating a new Piece, you should do so using it's * {@link SpannableTextView.BabushkaText.Piece.Builder}. * * Use this method if you want to modify the text of an existing Piece that is already displayed. After doing * so, you MUST call {@code display()} for the changes to show up. * * @param text the text to display */ public void setText(String text) { this.text = text; } /** * Sets the text color of this Piece. If you're creating a new Piece, you should do so using it's * {@link SpannableTextView.BabushkaText.Piece.Builder}. * * Use this method if you want to change the text color of an existing Piece that is already displayed. After * doing so, you MUST call {@code display()} for the changes to show up. * * @param color of text (it is NOT android Color resources ID, use getResources().getColor(R.color.colorId) for * it) */ public void setTextColor(int textColor) { this.textColor = textColor; } /** * Builder of Pieces */ public static class Builder { // required private final String text; // optional private int textSize = DEFAULT_ABSOLUTE_TEXT_SIZE; private int textColor = Color.BLACK; private int backgroundColor = -1; private float textSizeRelative = DEFAULT_RELATIVE_TEXT_SIZE; private int style = Typeface.NORMAL; private boolean underline = false; private boolean strike = false; private boolean superscript = false; private boolean subscript = false; /** * Creates a new Builder for this Piece. * * @param text the text of this Piece */ public Builder(String text) { this.text = text; } /** * Sets the absolute text size. * * @param textSize text size in pixels * @return a Builder */ public Builder textSize(int textSize) { this.textSize = textSize; return this; } /** * Sets the text color. * * @param textColor the color * @return a Builder */ public Builder textColor(int textColor) { this.textColor = textColor; return this; } /** * Sets the background color. * * @param backgroundColor the color * @return a Builder */ public Builder backgroundColor(int backgroundColor) { this.backgroundColor = backgroundColor; return this; } /** * Sets the relative text size. * * @param textSizeRelative relative text size * @return a Builder */ public Builder textSizeRelative(float textSizeRelative) { this.textSizeRelative = textSizeRelative; return this; } /** * Sets a style to this Piece. * * @param style see {@link android.graphics.Typeface} * @return a Builder */ public Builder style(int style) { this.style = style; return this; } /** * Underlines this Piece. * * @return a Builder */ public Builder underline() { this.underline = true; return this; } /** * Strikes this Piece. * * @return a Builder */ public Builder strike() { this.strike = true; return this; } /** * Sets this Piece as a superscript. * * @return a Builder */ public Builder superscript() { this.superscript = true; return this; } /** * Sets this Piece as a subscript. * * @return a Builder */ public Builder subscript() { this.subscript = true; return this; } /** * Creates a {@link SpannableTextView.BabushkaText.Piece} with the customized parameters. * * @return a Piece */ public Piece build() { return new Piece(this); } } } }
试着结合这个类Phrase.java那就更爽了;
ColorPhrase实现处理带颜色的字符串
https://github.com/THEONE10211024/ColorPhrase
https://github.com/quiqueqs/BabushkaText
Spanny实现字符串样式处理
https://github.com/binaryfork/Spanny
发表评论
-
NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端
2018-10-31 20:45 6781网上有一个StickyScrollView,称之为粘性Scro ... -
自定义Behavior实现AppBarLayout越界弹性效果
2017-03-31 09:33 10208一、继承AppBarLayout.Beha ... -
Android - 一种相似图片搜索算法的实现
2017-03-31 09:33 2563算法 缩小尺寸。 将图片缩小到8x8的尺寸,总共64个 ... -
使用SpringAnimation实现带下拉弹簧动画的 ScrollView
2017-03-30 11:30 2782在刚推出的 Support Library 25.3.0 里面 ... -
Android为应用添加角标(Badge)
2017-03-30 11:21 60911.需求简介 角标是什么意思呢? 看下图即可明了: 可 ... -
Android端与笔记本利用局域网进行FTP通信
2017-03-23 10:17 917先看图 打开前: 打开后: Activity类 ... -
PorterDuffColorFilter 在项目中的基本使用
2017-03-03 10:58 1288有时候标题栏会浮在内容之上,而内容会有颜色的变化,这时候就要求 ... -
ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
2017-02-24 09:41 2162用法在注释中: import android.anima ... -
迷你轻量级全方向完美滑动处理侧滑控件SlideLayout
2017-01-16 16:53 2538纯手工超级迷你轻量级全方向完美滑动处理侧滑控件(比官方 sup ... -
Effect
2017-01-05 09:57 0https://github.com/JetradarMobi ... -
动态主题库Colorful,容易地改变App的配色方案
2016-12-27 14:49 2509Colorful是一个动态主题库,允许您很容易地改变App的配 ... -
对视图的对角线切割DiagonalView
2016-12-27 14:23 1061提供对视图的对角线切割,具有很好的用户定制 基本用法 ... -
仿淘宝京东拖拽商品详情页上下滚动黏滞效果
2016-12-26 16:53 3408比较常用的效果,有现成的,如此甚好!:) import ... -
让任意view具有滑动效果的SlideUp
2016-12-26 09:26 1645基本的类,只有一个: import android.a ... -
AdvancedWebView
2016-12-21 09:44 16https://github.com/delight-im/A ... -
可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
2016-11-02 22:13 1853可设置圆角背景边框的的按钮, 通过调节色彩明度自动计算按下(p ... -
网络请求库相关
2016-10-09 09:35 62https://github.com/amitshekhari ... -
ASimpleCache一个简单的缓存框架
2015-10-26 22:53 2115ASimpleCache 是一个为android制定的 轻量级 ... -
使用ViewDragHelper实现的DragLayout开门效果
2015-10-23 10:55 3325先看一下图,有个直观的了解,向下拖动handle就“开门了”: ... -
保证图片长宽比的同时拉伸图片ImageView
2015-10-16 15:40 3671按比例放大图片,不拉伸失真 import android. ...
相关推荐
TextView的字体自适应控件的大小,根据网上的EditView的例子更改而来
Android 一个TextView中设置不同字体大小和颜色
一个textview里设置多种字体颜色,并添加图片
解决Android中原生TextView显示中英文特殊字符等末尾参差不齐问题
Android TextView 控件例子
同一个textview,字体颜色不一样 android中TextView分段显示不同颜色,字体,时间
如果我们的应用需要显示时间,可能首先想到大街上LED显示的绿色的发光字体, 想到 使用时钟控件显示时间,这种效果,我们用 TextView 就可以实现。具体看博客说明:编辑文章 - 博客频道 - CSDN.NET ...
TextView使用HTML处理字体样式、显示图片等
旋转TextView文字显示方向,可参考学习一下。
一个Textview的内容呈现多种颜色,任意控制其中的内容显示不同的颜色。
TextView设置字体格式以及滚动显示效果
下面的组件来实现RichText:也即在同一个TextView中设置不同的字体风格。对于某些应用,比如文本编辑,记事本,彩信,短信等地方,还必须使用这些组件才能达到想到的显示效果。 主要的基本工具类有android.text....
Android SpannableString实现TextView不同大小文字居中显示。
实现textview字体颜色的渐变显示
Android TextView 个别文字字体颜色格式
一个完整的代码类,主要是实现左右对齐的textview效果的控件,已经比较完善,有需要的可以下载参考一下
4、如果一切正常,那么你会在虚拟设备看到常用的控件(包括TextView, EditText,AutoCompleteTextView,MultiAutoCompleteTextView,Button,ImageButton,ToggleButton,CheckBox和RadioButton),根据中文提示操作。...
给TextView设置字体样式的三种方法
主要为大家详细介绍了TextView显示文本控件两种方法,TextView显示link的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
TextView超过最大行数显示更多,没有则全部显示,TextView的折叠功能实现,比较笨的方法,呵呵,有好的方法谢谢分享