我在我的strings.xml
以下行:
<string name="test_string">This is a <u>test</u></string>
在我的活动我的xml引用该字符串一个TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_string" />
古怪的是,当我的设备(小米弥A1,安卓8.0)上运行的应用程序,该<u>
前面还有空间被强调。注意“a”和“测试”(截图从实际设备)之间的空间下划线:
我曾尝试使用下面的strings.xml
也尝试:
<string name="test_string">This is a <u>test</u></string>
但结果是一样的。有任何想法吗?
我能够重现这对我的模拟器。要解决,我改变了字符串资源,如下所示:
<string name="underline">this is a <u>test</u></string>
然后,而不是简单的字符串设定为我的TextView的,我跑它通过Html.fromHtml()
第一:
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>
这将避免下划线的空间你<u>
标签前
我能解决这个使用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_view
。
这样一来,“a”和“测试”之间的空间没有下划线。
我能得到它与空间的XML字符工作。
<string name="test_string">This is a <u>test</u></string>