string.xml 中的粗体字符未显示在屏幕上
Bold characters in string.xml are not shown on screen
在我的 string.xml 文件中,我有这样的东西:
<string name="example_string"><b>This</b> is a <b>%1$s</b></string>
然后我将其放入 TextView 中:
textView.setText(getString(R.string.example_string, "good question"));
我传递给 getString()
方法的 "good question" 参数未显示在 粗体 中。连 "This" 这个词都没有加粗!
这是什么原因,如何解决?
============================================= ============================
我知道如何使用Html.fromHtml(),但此方法不支持将字符串插入到我在字符串资源中定义的占位符。如果你想告诉我 Html.fromHtml() 存在,请不要回复...
您需要使用 Html.fromHtml or using a SpannableString 来格式化文本。
Html.fromHtml
示例:
textView.setText(Html.fromHtml(getString(R.string.example_string, "good question")));
编辑:
如果您在传递标签时遇到问题,那是因为它需要被 CDATA
转义。
例如。 <string name="example_string">This is a <![CDATA[<b>%1$s</b>]]></string>
试试这个..
String myString = "This is a " + "<b>" + "your_string" + "</b>";
textview.setText(Html.fromHtml(myString));
有关更多信息,请参阅 here
像下面这样
String boldText = "Hello"+"<b>" + "Whosebug" + "</b>";
textView.setText(Html.fromHtml(boldText));
现在你的文字看起来像这样:你好Whosebug
看到这个link
use this... it will working.
/**
* Makes a substring of a string bold.
* @param text Full text
* @param textToBold Text you want to make bold
* @return String with bold substring
*/
public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){
SpannableStringBuilder builder=new SpannableStringBuilder();
if(textToBold.length() > 0 && !textToBold.trim().equals("")){
//for counting start/end indexes
String testText = text.toLowerCase(Locale.US);
String testTextToBold = textToBold.toLowerCase(Locale.US);
int startingIndex = testText.indexOf(testTextToBold);
int endingIndex = startingIndex + testTextToBold.length();
//for counting start/end indexes
if(startingIndex < 0 || endingIndex <0){
return builder.append(text);
}
else if(startingIndex >= 0 && endingIndex >=0){
builder.append(text);
builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0);
}
}else{
return builder.append(text);
}
return builder;
所以经过一天的搜索,我在 Android 开发者网站上找到了想要的答案!页面的 Link 在这里:https://developer.android.com/guide/topics/resources/string-resource.html
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.
基本上,我根据我原来的问题所做的更改是用 HTML 标记替换括号“<”,即 "(在你的 xml 文件中将它们一起输入!我不得不将它们分开,因为 Whosebug 会将标签显示为括号本身。)有关更详细的解释,请参阅 样式使用我在上面发布的网站的 HTML 标记 部分。
我已经成功使用 getText instead of getString。它似乎保持样式。
You can use either getString(int) or getText(int) to retrieve a
string. getText(int) retains any rich text styling applied to the
string.
在我的 string.xml 文件中,我有这样的东西:
<string name="example_string"><b>This</b> is a <b>%1$s</b></string>
然后我将其放入 TextView 中:
textView.setText(getString(R.string.example_string, "good question"));
我传递给 getString()
方法的 "good question" 参数未显示在 粗体 中。连 "This" 这个词都没有加粗!
这是什么原因,如何解决?
============================================= ============================
我知道如何使用Html.fromHtml(),但此方法不支持将字符串插入到我在字符串资源中定义的占位符。如果你想告诉我 Html.fromHtml() 存在,请不要回复...
您需要使用 Html.fromHtml or using a SpannableString 来格式化文本。
Html.fromHtml
示例:
textView.setText(Html.fromHtml(getString(R.string.example_string, "good question")));
编辑:
如果您在传递标签时遇到问题,那是因为它需要被 CDATA
转义。
例如。 <string name="example_string">This is a <![CDATA[<b>%1$s</b>]]></string>
试试这个..
String myString = "This is a " + "<b>" + "your_string" + "</b>";
textview.setText(Html.fromHtml(myString));
有关更多信息,请参阅 here
像下面这样
String boldText = "Hello"+"<b>" + "Whosebug" + "</b>";
textView.setText(Html.fromHtml(boldText));
现在你的文字看起来像这样:你好Whosebug
看到这个link
use this... it will working.
/**
* Makes a substring of a string bold.
* @param text Full text
* @param textToBold Text you want to make bold
* @return String with bold substring
*/
public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){
SpannableStringBuilder builder=new SpannableStringBuilder();
if(textToBold.length() > 0 && !textToBold.trim().equals("")){
//for counting start/end indexes
String testText = text.toLowerCase(Locale.US);
String testTextToBold = textToBold.toLowerCase(Locale.US);
int startingIndex = testText.indexOf(testTextToBold);
int endingIndex = startingIndex + testTextToBold.length();
//for counting start/end indexes
if(startingIndex < 0 || endingIndex <0){
return builder.append(text);
}
else if(startingIndex >= 0 && endingIndex >=0){
builder.append(text);
builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0);
}
}else{
return builder.append(text);
}
return builder;
所以经过一天的搜索,我在 Android 开发者网站上找到了想要的答案!页面的 Link 在这里:https://developer.android.com/guide/topics/resources/string-resource.html
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.
基本上,我根据我原来的问题所做的更改是用 HTML 标记替换括号“<”,即 "
我已经成功使用 getText instead of getString。它似乎保持样式。
You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string.