SpannableStringBuilder无法在TextView Android中运行

时间:2016-12-19 14:26:03

标签: android textview spannablestring

我在TextView设置单词的文字颜色时遇到困难。

以下是我的代码:

     final SpannableStringBuilder sb = new SpannableStringBuilder(first + next);

    // Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(0,0,255));

    // Span to make text bold
 final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);

    // Set the text color for first 4 characters
sb.setSpan(fcs, 0, first.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

    // make them also bold
 sb.setSpan(bss, 0, first.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  textView.setText(sb);

这是TextView的XML -

    <TextView
        android:id="@+id/comment_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:ellipsize="end"
        android:maxLines="5"
        android:textAllCaps="false"
        android:text="Lorem Ipsum is simply dummy text"
        android:textSize="@dimen/description_text" />

我的尝试 -

  1. 设置此但没有结果。

    机器人:textAllCaps =&#34;假&#34;

0 个答案:

没有答案