您的位置:首页 > 产品设计 > UI/UE

(一)SpannableString、SpannableStringBuilder的简单使用

2017-03-31 15:43 260 查看
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);

结果:



下篇(二)各种各样的Span

参考博客:http://blog.csdn.net/harvic880925/article/details/38984705#comments
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: