Android strings.xml:如何从<;u>;标签?

Android strings.xml:如何从<;u>;标签?,android,textview,android-styles,Android,Textview,Android Styles,我的strings.xml中有以下行: <string name="test_string">This is a <u>test</u></string> <string name="test_string">This is a&#032;<u>test</u></string> 但结果是一样的。有什么想法吗?我能用SpannableString解决这个问题。将以下内容添加到活动的Java

我的
strings.xml中有以下行:

<string name="test_string">This is a <u>test</u></string>
<string name="test_string">This is a&#032;<u>test</u></string>

但结果是一样的。有什么想法吗?

我能用SpannableString解决这个问题。将以下内容添加到活动的Java代码中:

TextView testView = findViewById(R.id.test_view);
SpannableString testContent = new SpannableString(getResources().getString(R.string.test_string));
// underlining only "test" in "this is a test"
// note that 10 is the character BEFORE the first char we want to underline
// and 14 is the last char we want to underline
testContent.setSpan(new UnderlineSpan(), 10, 14, 0);
testView.setText(testContent);
显然,在本例中,活动xml中的TextView
id
应该是
test\u view


这样,“a”和“test”之间的空格就不加下划线了。

我可以在模拟器上重现这一点。为了解决这个问题,我对字符串资源进行了如下更改:

<string name="underline">this is a &lt;u>test&lt;/u></string>

我能够让它与空间的XML字符一起工作

这是一个 ;测试

您的字符串必须用
字符包围

<string name="test_string">"This is a <u>test</u>"</string>
“这是一个测试”

它将避免在你的
标签前面加下划线的空格

你试过SpannableString吗?这很有效。我是Android开发的新手,以前从未使用过SpannableString,但多亏了你的评论和一些谷歌搜索,我才能够让它工作。还发布了详细的答案,以防有人遇到类似的问题lem.Yep,这是可行的。事实上,我更喜欢这个解决方案,而不是我在自己的答案中使用SpannableString提出的解决方案。在你的答案中,你不必为每个字符串指定开始和结束字符的位置。这使得它在有多个带下划线的字符串需要处理的情况下更有用。请注意,fromHtml方法是deprecated now。现在必须将fromHtml方法与fromHtml(字符串源,int标志)或fromHtml(字符串源,int标志,Html.ImageGetter ImageGetter,Html.TagHandler TagHandler)一起使用。这是一个测试。注释中缺少换行符是非常不幸的,但我不想发布一个新的答案,只是为了指出您可以从HTML向XML教授字符实体名称。此代码仅适用于该特定字符串。确实,您可以基于该语言指定开始索引和结束索引。如果我们需要设置一个粗体风格,从法语、德语等的另一个索引开始?将会发生变化
TextView text = findViewById(R.id.text);
String withMarkup = getString(R.string.underline);
text.setText(Html.fromHtml(withMarkup));
<string name="test_string">"This is a <u>test</u>"</string>