SpannableString、SpannableStringBuilder的简单使用

1.SpannableString和SpannableStringBuilder的区别:

    类似于String和StringBuilder的区别,SpannableStringBuilder可以通过append()方法来拼接多个String。

例如:

xml

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pistol.spannablestringbuildertest.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.433"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.562"/>

</android.support.constraint.ConstraintLayout>

activity

 

 

public class MainActivity extends AppCompatActivity {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);

        SpannableString spannableString = new SpannableString("一次性传入内容");

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        spannableStringBuilder.append("多次");
        spannableStringBuilder.append("传入");

        tv1.setText(spannableString);
        tv2.setText(spannableStringBuilder);

        spannableStringBuilder.append("内容");
        tv3.setText(spannableStringBuilder);

    }
}

2.要设置多种样式就需要用到setSpan()方法:

 

 

/**
 * Mark the specified range of text with the specified object.
 * The flags determine how the span will behave when text is
 * inserted at the start or end of the span's range.
 */

 

//        使用指定的对象标记指定的文本范围。
//        标志确定在跨度范围的开始或结束时插入文本时跨度的行为。

 

public void setSpan(Object what, int start, int end, int flags) { setSpan(true, what, start, end, flags);}

 

what 中传入的是各种的span;

start是span的起始位置;

end是span的结束为止;

flags对后续相邻插入字符影响的标识(对相邻插入的字符是否使用相同的样式);

flags的四中标识:

 

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE//指定范围前后相邻字符都不使用相同样式			--(a,b)
Spannable.SPAN_INCLUSIVE_INCLUSIVE//指定范围前后相邻字符都使用相同样式				--[a,b]
Spannable.SPAN_INCLUSIVE_EXCLUSIVE//指定范围位置前相邻的字符使用相同样式,后面不使用此样式	--[a,b)
Spannable.SPAN_EXCLUSIVE_INCLUSIVE//指定范围位置前相邻的字符不使用相同样式,后面使用此样式	--(a,b]

例如:

 

 

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
//文字改变颜色
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
spannableStringBuilder.append("分多次");
spannableStringBuilder.append("传入的");
//正长展示
tv2.setText(spannableStringBuilder);
//指定范围及标识,指定范围位置(第4、5、6个字符,从零开始计算)前相邻的字符不使用相同样式,后面使用此样式
spannableStringBuilder.setSpan(span, 4, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableStringBuilder.append("内容");
tv3.setText(spannableStringBuilder);

结果:

 

 

参考博客:http://blog.csdn.net/harvic880925/article/details/38984705#comments

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值