由于HTML在字符串资源中的使用,我不能将其从charsequence转换为string (否则我将丢失格式)。
<string name="exponent_key">x<sup><small>y</small></sup>/string>在使用getString()之后,我想用'other stuff‘替换'y’,但是你怎么做呢?这似乎是一个简单的问题,但由于某些原因,我找不到任何关于它的东西。
编辑:考虑到这一点,我是否可以将charsequence转换为包含HTML代码的字符串,然后再将其转换回charsequence?
编辑:忘记提到字符串被设置为按钮标题,然后被检索(在那里使用它)。
发布于 2015-08-24 16:39:39
的确有。创建一个函数,其中您接受一个需要格式化的字符串作为参数。在函数中,您只需通过迭代器获取它,然后再使用Html.fromHtml()
发布于 2015-08-24 16:42:15
让我们将您的问题分成多个步骤:
String.format("%1$","otherStuff");
%1$ - String string = getString(R.string.exponent_key,"otherStuff");
%1$:%2$ - getString(R.string.string_name,new String[]{"hello",“world”});
所以,在你的XML中有了上面的字符串,只需要使用
HTML (R.string.exponent_key,"otherStuff");
textView.setText(Html.fromHtml(getString(R.string.exponent_key,"otherStuff")));
发布于 2015-08-24 16:57:39
在你的string.xml中
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>在代码中
textView.setText(getString(R.string.exponent_key,2))https://stackoverflow.com/questions/32177493
复制相似问题