Android – highlight text in textview

I have a textview with random background color (possibly any color). I also have a text that needs to be readable on the textview. I think the best solution is to highlight the text in white and set the text color to black

My question is: is it possible to highlight text in textview from XML?

My layout includes the following:

  <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/colorButton4"
        android:layout_gravity="right|bottom"
        android:background="@drawable/layout_border"
        android:layout_marginRight="30dp"
        android:layout_marginBottom ="30dp"
        android:clickable="true"
        android:onClick="onClick"
        android:gravity="center"
        android:textColorHighlight="@color/bgWhite"
        android:textColor="@color/Black"
        android:text="5431354" />

But it does not emphasize the text

resolvent:

You may want to use spannablestring for this purpose, which allows various parts of the string to be rendered in different ways in textview

like this:

    SpannableString str = new SpannableString("Highlighted. Not highlighted.");
    str.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 11, 0);
    textView.setText(str);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>