为textView Android设置文本颜色

kav*_*vya 9 android colors textview

在string.xml文件中,我使用以下标记

 <string name="CodeColor" >"#0000ff"</string>
Run Code Online (Sandbox Code Playgroud)

如果我使用

 textview1.setTextColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

它有效,但是当我使用时

  textview1.setTextColor(TextViewStyles.this.getResources().getColor(R.string.CodeColor)); 

 or
 textview1.setTextColor(R.string.CodeColor);
Run Code Online (Sandbox Code Playgroud)

它不起作用.有什么建议...

提前致谢

小智 16

您需要在xml中创建一组样式(定期在res/values/styles.xml中)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="gray">#eaeaea</color>
    <color name="titlebackgroundcolor">#00abd7</color>
    <color name="titlecolor">#666666</color>
<resources>
Run Code Online (Sandbox Code Playgroud)

在布局文件中,您可以调用颜色或样式:

android:textColor="@color/titlecolor"
Run Code Online (Sandbox Code Playgroud)

查看一些示例:

http://developer.android.com/guide/topics/ui/themes.html


Ran*_*oid 12

您可以使用

  textView1.setTextColor(getResources().getColor(R.color.mycolor))
Run Code Online (Sandbox Code Playgroud)

要么

  textview1.setBackgroundColor(Color.parseColor("#ffffff"));
Run Code Online (Sandbox Code Playgroud)

要么

    textview1.setBackgroundColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

要么

    textView1.setBackgroundColor(R.color.black);
Run Code Online (Sandbox Code Playgroud)


Ped*_*ito 5

这可能更容易:

TextView textresult = (TextView)findViewById(R.id.textView1);
textresult.setTextColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)