这个控件本人强烈推荐,它会使得布局非常的简单且高效;
下面这个布局如果是你,你会用多少层?多少控件生成?
告诉你吧,一个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
用法也很简单:
- public void createSpannableTextView(SpannableTextView tv, String title, String content)
- {
-
-
- tv.reset();
-
- 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());
-
-
- 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());
-
-
- 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;
-
-
- 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;
-
- public class SpannableTextView extends TextView
- {
-
-
- private static int DEFAULT_ABSOLUTE_TEXT_SIZE;
-
- private static float DEFAULT_RELATIVE_TEXT_SIZE = 1;
-
- private List<Piece> mPieces;
-
-
- 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();
- }
-
-
- public void addPiece(Piece aPiece)
- {
- mPieces.add(aPiece);
- }
-
-
- public void addPiece(Piece aPiece, int location)
- {
- mPieces.add(location, aPiece);
- }
-
-
- public void replacePieceAt(int location, Piece newPiece)
- {
- mPieces.set(location, newPiece);
- }
-
-
- public void removePiece(int location)
- {
- mPieces.remove(location);
- }
-
-
- public void clearPiece()
- {
- mPieces.clear();
- }
-
-
- public Piece getPiece(int location)
- {
- if (location >= 0 && location < mPieces.size())
- {
- return mPieces.get(location);
- }
-
- return null;
- }
-
-
- public void display()
- {
-
-
- StringBuilder builder = new StringBuilder();
- for (Piece aPiece : mPieces)
- {
- builder.append(aPiece.text);
- }
-
-
- 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();
- }
-
-
- 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);
- }
-
-
- finalString.setSpan(new StyleSpan(aPiece.style), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
-
-
- finalString.setSpan(new AbsoluteSizeSpan(aPiece.textSize), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
-
-
- finalString.setSpan(new RelativeSizeSpan(aPiece.textSizeRelative), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
-
-
- finalString.setSpan(new ForegroundColorSpan(aPiece.textColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
-
-
- if (aPiece.backgroundColor != -1)
- {
- finalString.setSpan(new BackgroundColorSpan(aPiece.backgroundColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- }
-
-
- public void reset()
- {
- mPieces = new ArrayList<Piece>();
- setText("");
- }
-
-
- public void changeTextColor(int textColor)
- {
- for (Piece mPiece : mPieces)
- {
- mPiece.setTextColor(textColor);
- }
- display();
- }
-
-
- 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;
- }
-
-
- public void setText(String text)
- {
- this.text = text;
- }
-
-
- public void setTextColor(int textColor)
- {
- this.textColor = textColor;
- }
-
-
- public static class Builder
- {
-
-
- private final String text;
-
-
- 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;
-
-
- public Builder(String text)
- {
- this.text = text;
- }
-
-
- public Builder textSize(int textSize)
- {
- this.textSize = textSize;
- return this;
- }
-
-
- public Builder textColor(int textColor)
- {
- this.textColor = textColor;
- return this;
- }
-
-
- public Builder backgroundColor(int backgroundColor)
- {
- this.backgroundColor = backgroundColor;
- return this;
- }
-
-
- public Builder textSizeRelative(float textSizeRelative)
- {
- this.textSizeRelative = textSizeRelative;
- return this;
- }
-
-
- public Builder style(int style)
- {
- this.style = style;
- return this;
- }
-
-
- public Builder underline()
- {
- this.underline = true;
- return this;
- }
-
-
- public Builder strike()
- {
- this.strike = true;
- return this;
- }
-
-
- public Builder superscript()
- {
- this.superscript = true;
- return this;
- }
-
-
- public Builder subscript()
- {
- this.subscript = true;
- return this;
- }
-
-
- 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
高效快捷解决一个TextView显示多种字体的控件SpannableTextView
原文:http://www.cnblogs.com/gao-chun/p/4849348.html