SpannableStringBuilder在Android的TextView中不起作用

问题描述 投票:2回答:1

我在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. 设置此项,但无结果。

    android:textAllCaps =“ false”

android textview spannablestring
1个回答
0
投票

如果您在设备中使用自定义字体。我认为有一个愚蠢的错误。请在您的设备中将自定义字体更改为默认字体,然后重试。

© www.soinside.com 2019 - 2024. All rights reserved.