Android 神奇的SpannableStringBuilder

一 無圖言屌 先看看神奇的效果 僅用一個TextView實現android

 

二 SpannableStringBuilderide

Google官方介紹字體

This is the class for text whose content and markup can both be changed.ui

翻譯過來this

這是用於文本的類,其內容和標記均可以更改。spa

繼承關係翻譯

SpannableStringBuilder實現了CharSequence 所以 能夠直接在TextView#setText()中使用3d

 

三 用法code

在xml中建立一個TextViewxml

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginHorizontal="60dp"
    android:textSize="16sp" />

1. 字體顏色

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#0094FF"));
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

2. 背景顏色

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
BackgroundColorSpan span = new BackgroundColorSpan(Color.parseColor("#0094FF"));
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

3. 字體大小 絕對

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
AbsoluteSizeSpan span = new AbsoluteSizeSpan(ScreenUtils.sp2px(20));
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

4. 字體大小 相對

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
RelativeSizeSpan span = new RelativeSizeSpan(0.5F);
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

5. 字體風格

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
StyleSpan span = new StyleSpan(Typeface.BOLD); //Typeface.BOLD=粗體 Typeface.ITALIC=斜體 Typeface.BOLD_ITALIC=粗斜體
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

6. 刪除線

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
StrikethroughSpan span = new StrikethroughSpan();
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

7. 下劃線

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
UnderlineSpan span = new UnderlineSpan();
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

8. 圖片 原始大小

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
ImageSpan span = new ImageSpan(this, R.mipmap.ic_launcher);
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

9. 圖片 控制大小

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, ScreenUtils.dp2px(32), ScreenUtils.dp2px(16));
ImageSpan span = new ImageSpan(drawable);
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);

10. 點擊事件

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
ClickableSpan span = new ClickableSpan() {
    @Override
    public void onClick(@NonNull View view) {
        Toast.makeText(view.getContext(), "onClick", Toast.LENGTH_LONG).show();
    }

    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.parseColor("#0094FF"));
        ds.setUnderlineText(true); //設置下劃線
        ds.setFakeBoldText(true); //設置粗體
    }
};
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);
// 去除點擊時的背景色
textView.setHighlightColor(ContextCompat.getColor(textView.getContext(), android.R.color.transparent));
// 點擊事件生效
textView.setMovementMethod(LinkMovementMethod.getInstance());

11. 點擊事件+圖片

TextView textView = findViewById(R.id.text);
SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
ClickableSpan span = new ClickableSpan() {
    @Override
    public void onClick(@NonNull View view) {
        Toast.makeText(view.getContext(), "onClick", Toast.LENGTH_LONG).show();
    }

    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.parseColor("#0094FF"));
        ds.setUnderlineText(true); //設置下劃線
        ds.setFakeBoldText(true); //設置粗體
    }
};
builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
ImageSpan imageSpan = new ImageSpan(this, R.mipmap.ic_launcher);
builder.setSpan(imageSpan, 5, 6, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含頭不含尾
textView.setText(builder);
// 去除點擊時的背景色
textView.setHighlightColor(ContextCompat.getColor(textView.getContext(), android.R.color.transparent));
// 點擊事件生效
textView.setMovementMethod(LinkMovementMethod.getInstance());
相關文章
相關標籤/搜索