我正在尝试将上标和下标文本插入到 android 按钮文本中。 我尝试在 strings.xml 文件中提供以下内容
<string name="NAME"><![CDATA[<sup><small>-1</small></sup>]]></string>
在 MainActivity.java 文件中,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button NAME = (Button) findViewById(R.id.button10);
NAME.setText(Html.fromHtml(getResources().getString(R.string.NAME)));
}
但我无法成功。
查询:
- 有没有办法在“strings.xml”文件本身中指定它,而无需在代码中使用 setText ?
最佳答案
问题: 您获取的值错误。
解决方案:
使用 R.string.NAME
代替 R.string.iHyperbolic
您的NAME
包含html字符串,而不是iHyperbolic
。
Is there any way to specify it in "strings.xml" file itself without using setText in code ?
据我所知,在 Html 类处理完 html 字符串后,您必须使用 setText() 来设置它。因为 Html 类将 HTML 字符串处理为可显示的样式文本。如果您希望在小部件上显示此 Html 样式文本,则必须使用 setText()。
关于java - Android 按钮文本和 HTML 上标和下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24483573/