strings.xml中:如何删除从之前的空间下划线 标签?

问题描述 投票:3回答:4

我在我的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”和“测试”(截图从实际设备)之间的空间下划线:

device

我曾尝试使用下面的strings.xml也尝试:

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

但结果是一样的。有任何想法吗?

android textview android-styles
4个回答
5
投票

我能够重现这对我的模拟器。要解决,我改变了字符串资源,如下所示:

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

然后,而不是简单的字符串设定为我的TextView的,我跑它通过Html.fromHtml()第一:

TextView text = findViewById(R.id.text);
String withMarkup = getString(R.string.underline);
text.setText(Html.fromHtml(withMarkup));

2
投票

你的字符串必须与"字符包围

<string name="test_string">"This is a <u>test</u>"</string>

这将避免下划线的空间你<u>标签前


1
投票

我能解决这个使用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”和“测试”之间的空间没有下划线。


1
投票

我能得到它与空间的XML字符工作。

<string name="test_string">This is a&#160;<u>test</u></string>
© www.soinside.com 2019 - 2024. All rights reserved.