字体样式。
构造函数:
示例1:
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(context, R.style.xx);
String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(textAppearanceSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);
示例2:
Parcel parcel = Parcel.obtain();
parcel.writeString("SERIF");
parcel.writeInt(Typeface.BOLD_ITALIC);
parcel.writeInt(18);
try {
ColorStateList colorList = ColorStateList.createFromXml(context.getResources(), context.getResources().getXml(R.drawable.color_list));
parcel.writeInt(1);
colorList.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
parcel.writeInt(1);
colorList.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
} catch (Exception e) {
e.printStackTrace();
}
parcel.setDataPosition(0);
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(parcel);
String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(textAppearanceSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);
示例3:
重写updateDrawState(TextPaint textPaint)
方法,修改字体的样式、文本颜色:
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, R.style.xx) {
@Override
public void updateDrawState(TextPaint textPaint) {
super.updateDrawState(textPaint);
textPaint.setColor(Color.argb(255, 54, 92, 124));
}
};
String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(typefaceSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);