【问题标题】:Android TextView underline works notAndroid TextView 下划线不起作用
【发布时间】:2018-08-18 14:02:37
【问题描述】:

我想做一件简单的事情——在 TextView 中为文本添加下划线。字符串放在strings.xml中(因为翻译):

<string name="mytext"><u>UnderlineText</u></string>

然后我以编程方式放置文本:

 TextView txt1 = findViewById(R.id.autor);
 txt1.setText(resources.getString(R.string.mytext));

但是,由于某种原因,字符串文件中的 &lt;u&gt; 被忽略,我的文本没有下划线。

我也试过了:

 txt1.setText(Html.fromHtml(resources.getString(R.string.mytext)));

还是什么都没有。文本正常显示,没有下划线。

【问题讨论】:

    标签: android textview underline


    【解决方案1】:

    替换这个:

    &lt;string name="mytext"&gt;&lt;![CDATA[&lt;u&gt;UnderlineText&lt;/u&gt;]]&gt; &lt;/string&gt;

    【讨论】:

    • 啊,我不相信我以前没有尝试过这个。工作正常。谢谢!
    【解决方案2】:

    尝试使用 getText() 而不是 getString()。

    txt1.setText(resources.getText(R.string.mytext));
    

    这会返回包含样式的本地化字符串。

    【讨论】:

    • 按照你的方式,我们不需要使用 Html.fromHtml 或 SpannableString...谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2013-09-29
    • 1970-01-01
    • 2011-12-23
    • 2020-02-01
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多