更改字符串文本颜色

时间:2011-11-25 16:15:11

标签: android

<string name="no_city"><font color='#FF0000'><b>Please select your location under Preferences in the menu</b></font> </string>
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contents);
    this.setContentView(R.layout.contents);

    ChangeLog cl = new ChangeLog(this);
    if (cl.firstRun())
        cl.getFullLogDialog().show();


    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    TextView text1 = (TextView)findViewById(R.id.textview1);

    Resources res = getResources();
    String text = String.format(res.getString(R.string.no_city));
    CharSequence styledText = Html.fromHtml(text);

    text1.setText(prefs.getString("listPref",res.getString(R.string.no_city)));

我正在尝试更改no_city字符串中文本的颜色。我有这样的方式我输入了顶部的字符串和下面的java。 Ihav尝试了十六进制和颜色名称,但都没有工作。我在Google的开发者网站上发现了这些信息,如何引用这些更改,它编译得很好,但文本仍然是白色的。如果列表中没有选择任何内容,我将使用带有列表首选项的字符串作为默认显示的字符串。当用户进行选择时,文本需要为白色,因此我无法更改xml或java文件中的整个文本视图颜色。我有一个感觉我错过了一些小东西或我忽略了什么,但我无法追踪它。

3 个答案:

答案 0 :(得分:0)

你试过text1.setTextColor() ???

答案 1 :(得分:0)

在视图上设置文本时,将SPANNABLE指定为BufferType:

text1.setText(styledText, TextView.BufferType.SPANNABLE);

答案 2 :(得分:0)

你可以直接使用:

txt.setText(Html.fromHtml(getResources().getString(R.string.no_city));

参考this

相关问题