Android Html.fromHtml(String) doesn't work for text

后端 未结 12 1455
逝去的感伤
逝去的感伤 2020-12-23 19:32

I am trying to add lines with different colors to my TextView using html tags.

For whatever reason,

    Html.fromHtml(\"

        
相关标签:
12条回答
  • 2020-12-23 20:02

    Make sure to turn off any modifiers like:

    android:textAllCaps="true"
    
    0 讨论(0)
  • 2020-12-23 20:07

    My answer involves guesswork about your code, but here goes:

    When you use the font tag: DO NOT include an alpha channel so that your hex string looks like "#ff123456". If you use Integer.toHexString(), you will have an alpha channel in that result.

    It worked when i used substring(2) on my hex string from rescource.

    To sum up:

    text.setText(Html.fromHtml("<font color='#123456'>text</font>"));
    

    will work, but:

    text.setText(Html.fromHtml("<font color='#ff123456'>text</font>"));
    

    won't!

    0 讨论(0)
  • 2020-12-23 20:07

    That looks like a very dark color, are you sure that your screen is capable to display such colors, so you can distinguish them from black? The code snippet looks good, I've tried similar code many times and it worked like a charm. Try it with somewhat brighter, i.e. #ff0000 (red), to verify that it works:

    TextView text = ... // find or instantinate your text view.
    text.setText(Html.fromHtml("<font color='#ff0000'>text</font>"));
    
    0 讨论(0)
  • 2020-12-23 20:07

    Yeah I agree, it doesn't work sometimes.

    As an alternative, I use in xml for Textview:

    android:textColorLink="yourColor"
    

    works like a charm ;)

    0 讨论(0)
  • 2020-12-23 20:10

    The fromHtml method is extremely limited in terms of the HTML tags that it supports, and font is not one of them. See http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html for an unofficial list. I did some research on this myself, and I found that fromHtml is based on an obscure and poorly documented rendering engine.

    0 讨论(0)
  • 2020-12-23 20:11

    try this and it should works

     textView.setText(Html.fromHtml("<font color=\"#145A14\">text</font>"));
    
    0 讨论(0)
提交回复
热议问题