(Android) 更改 strings.xml 中的文本颜色
(Android) Changing text color in strings.xml
首先,根据用户的操作,我想从我的 strings.xml 资源文件中检索某些字符串:
String option1 = context.getString(R.string.string_one)
String option2 = context.getString(R.string.string_two)
String option3 = context.getString(R.string.string_three)
然后,我将这些字符串作为 String[] options
传递给自定义 adapter
以获得 ListView
我在哪里设置 TextView
的文本
public ChoicesAdapter(Context context, String[] options) {
super(context, R.layout.choice_option_layout_2,choices);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater MyInflater = LayoutInflater.from(getContext());
View MyView = MyInflater.inflate(R.layout.option_list_layout, parent, false);
String option = getItem(position);
TextView textView = (TextView) MyView.findViewById(R.id.textView);
textView.setText(Html.fromHtml(option));
return MyView;
}
我希望 strings.xml 文件中的不同字符串具有不同的颜色或不同的格式。例如,这是我的字符串之一:
<string name ="exit"><![CDATA[<i>exit</i>]]></string>
但是,当这个字符串在屏幕上显示时,显示为:"<i>exit</i>"
所以,我猜我的方法中某处丢失了 string.xml 资源的格式。我怎样才能得到它而不是显示 "<i>exit</i>"
,它会在屏幕上显示“exit”?
我想我的问题是我在哪里使用 .getString()
。这是否以某种方式忽略了我在 .xml 文件中添加的格式?
查看 http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling - 他们的例子是:
<string name="welcome">Welcome to <b>Android</b>!</string>
它说您可以对 粗体 文本使用 <b>text</b>
,对 斜体 文本使用 <i>text</i>
,以及 <u>text</u>
用于带下划线的文本。
其中的重要部分是,"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."
他们说"store your styled text resource as an HTML-escaped string"喜欢
<string name="exit"><i>exit</i></string>
然后使用:
Resources res = getResources();
String text = String.format(res.getString(R.string.exit));
CharSequence styledText = Html.fromHtml(text);
正确获取格式化文本。
您刚刚尝试将字符串读入 Spannable 吗?
// Use a spannable to keep formatting
Spannable mySpannable = Html.fromHtml(context.getString(R.string.string_one));
textView.setText(mySpannable);
首先,根据用户的操作,我想从我的 strings.xml 资源文件中检索某些字符串:
String option1 = context.getString(R.string.string_one)
String option2 = context.getString(R.string.string_two)
String option3 = context.getString(R.string.string_three)
然后,我将这些字符串作为 String[] options
传递给自定义 adapter
以获得 ListView
我在哪里设置 TextView
public ChoicesAdapter(Context context, String[] options) {
super(context, R.layout.choice_option_layout_2,choices);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater MyInflater = LayoutInflater.from(getContext());
View MyView = MyInflater.inflate(R.layout.option_list_layout, parent, false);
String option = getItem(position);
TextView textView = (TextView) MyView.findViewById(R.id.textView);
textView.setText(Html.fromHtml(option));
return MyView;
}
我希望 strings.xml 文件中的不同字符串具有不同的颜色或不同的格式。例如,这是我的字符串之一:
<string name ="exit"><![CDATA[<i>exit</i>]]></string>
但是,当这个字符串在屏幕上显示时,显示为:"<i>exit</i>"
所以,我猜我的方法中某处丢失了 string.xml 资源的格式。我怎样才能得到它而不是显示 "<i>exit</i>"
,它会在屏幕上显示“exit”?
我想我的问题是我在哪里使用 .getString()
。这是否以某种方式忽略了我在 .xml 文件中添加的格式?
查看 http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling - 他们的例子是:
<string name="welcome">Welcome to <b>Android</b>!</string>
它说您可以对 粗体 文本使用 <b>text</b>
,对 斜体 文本使用 <i>text</i>
,以及 <u>text</u>
用于带下划线的文本。
其中的重要部分是,"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."
他们说"store your styled text resource as an HTML-escaped string"喜欢
<string name="exit"><i>exit</i></string>
然后使用:
Resources res = getResources();
String text = String.format(res.getString(R.string.exit));
CharSequence styledText = Html.fromHtml(text);
正确获取格式化文本。
您刚刚尝试将字符串读入 Spannable 吗?
// Use a spannable to keep formatting
Spannable mySpannable = Html.fromHtml(context.getString(R.string.string_one));
textView.setText(mySpannable);