(一)SpannableString、SpannableStringBuilder的简单使用
2017-03-31 15:43
260 查看
1.SpannableString和SpannableStringBuilder的区别:
类似于String和StringBuilder的区别,SpannableStringBuilder可以通过append()方法来拼接多个String。
例如:
xml
activity
2.要设置多种样式就需要用到setSpan()方法:
// 标志确定在跨度范围的开始或结束时插入文本时跨度的行为。
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的四中标识:
例如:
结果:
下篇(二)各种各样的Span
参考博客:http://blog.csdn.net/harvic880925/article/details/38984705#comments
类似于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
相关文章推荐
- android - SpannableString或SpannableStringBuilder以及string.xml文件中的整型和string型代替
- iOS开发-String和NSArray的简单使用
- SpannableStringBuilder的使用方法
- 使用StringBuilder, StringBuilder和String的区别
- android SpannableString 使用详解(给文本添加效果)
- string的简单使用
- String、StringBuffer、StringBuilder的区别及使用
- 简单看看这两个类 String和StringBuilder
- 如何选择使用String、StringBuffer与StringBuilder
- 使用android SpannableStringBuilder实现图文混排
- 关于String和StringBuilder、StringBuffer的一个简单性能测试
- String 和 StringBuilder 的区别和使用环境
- C#中BitConverter.ToUInt16()和BitConverter.ToString()的简单使用
- android - SpannableString或SpannableStringBuilder以及string.xml文件中的整型和string型代替
- android - SpannableString或SpannableStringBuilder以及string.xml文件中的整型和string型代替
- 朝花夕拾:Java中String,StringBuffer,StringBuilder使用方法以及异同比较
- Java高效运行必备:如何选择使用String、StringBuffer与StringBuilder
- (原创)如何在spannableString中使用自定义字体
- Android string-array数据源简单使用
- java中String、StringBuffer、StringBuilder的区别 java中String、StringBuffer、StringBuilder是编程中经常使用的字符串类,他们之间的区