【问题标题】:How to change text background inside in xml如何在xml中更改文本背景
【发布时间】:2016-10-12 20:16:34
【问题描述】:

假设我在字符串 XML 中有下一个代码,它工作正常,输出时文本颜色变为蓝色

<string name="hello_world">
    <font color='blue'> Hello world!</font> here is my text with yellow background 
</string>

问题 - 是否可以以与上述类似的方式为某些世界更改 文本背景直接在 xml 字符串中

最终结果:

【问题讨论】:

    标签: android xml string text background


    【解决方案1】:

    好的,请检查:

    <string name="hello_world">
    <font color='blue'> Hello world!</font><font color = 'black' bgcolor = 'yellow'> here is my text with yellow background</font>
    </string>
    

    【讨论】:

    • 我只需要更改一些 WORDS,因为 TextView 位于 ListView 中
    • 是的,如果文本直接在 TextView 中,则以这种方式工作。
    • 但是如果我通过适配器将字符串变量传递到数组列表中不起作用...非常有趣,... FONT COLOR 工作良好,但 BGCOLOR 不行...
    【解决方案2】:

    是的 - 只需在 xml 字符串中添加正确的 html 标记(就像您所做的那样),然后将文本设置为 TextView,如下所示:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        yourTextView.setText(Html.fromHtml(getString(R.string.your_string), Html.FROM_HTML_MODE_LEGACY));
    } else {
        yourTextView.setText(Html.fromHtml(getString(R.string.your_string)));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多