strings.xml:如何去除 <u> 标签前 space 的下划线?
strings.xml: How to remove underlining from the space preceding an <u> tag?
我的 strings.xml
中有以下行:
<string name="test_string">This is a <u>test</u></string>
在我的 activity xml 中,我在 TextView 中引用了这个字符串:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_string" />
奇怪的是,当我 运行 我设备上的应用程序(小米 Mi A1,Android 8.0)时,<u>
之前的 space 也带有下划线。注意"a"和"test"之间带下划线的space(实际设备截图):
我也尝试在 strings.xml
中使用以下内容:
<string name="test_string">This is a <u>test</u></string>
但是结果是一样的。有什么想法吗?
我能够使用 SpannableString 解决这个问题。在 activity 的 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);
显然,在这个例子中你的 TextView id
in activity xml 应该是 test_view
.
这样,"a"和"test"之间的space就没有下划线了。
我能够在我的模拟器上重现这个。为了解决,我改变了字符串资源如下:
<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));
我能够让它与 space 的 XML 字符一起使用。
<string name="test_string">This is a <u>test</u></string>
您的字符串必须用 "
字符包围
<string name="test_string">"This is a <u>test</u>"</string>
它将避免 <u>
标签前带下划线的 space
我的 strings.xml
中有以下行:
<string name="test_string">This is a <u>test</u></string>
在我的 activity xml 中,我在 TextView 中引用了这个字符串:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_string" />
奇怪的是,当我 运行 我设备上的应用程序(小米 Mi A1,Android 8.0)时,<u>
之前的 space 也带有下划线。注意"a"和"test"之间带下划线的space(实际设备截图):
我也尝试在 strings.xml
中使用以下内容:
<string name="test_string">This is a <u>test</u></string>
但是结果是一样的。有什么想法吗?
我能够使用 SpannableString 解决这个问题。在 activity 的 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);
显然,在这个例子中你的 TextView id
in activity xml 应该是 test_view
.
这样,"a"和"test"之间的space就没有下划线了。
我能够在我的模拟器上重现这个。为了解决,我改变了字符串资源如下:
<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));
我能够让它与 space 的 XML 字符一起使用。
<string name="test_string">This is a <u>test</u></string>
您的字符串必须用 "
字符包围
<string name="test_string">"This is a <u>test</u>"</string>
它将避免 <u>
标签前带下划线的 space