为 textView Android 设置文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12524920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:52:41  来源:igfitidea点击:

Set Text Color for textView Android

androidcolorstextview

提问by kavya

In the string.xml file i use the following tag

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

 <string name="CodeColor" >"#0000ff"</string>

If I use

如果我使用

 textview1.setTextColor(Color.RED);

it works, but when I use

它有效,但是当我使用

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

 or
 textview1.setTextColor(R.string.CodeColor);

it doen't work. Any suggestions...

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

Thanks in Advance

提前致谢

回答by

You need to create a set of styles in your xml (regularly in res/values/styles.xml)

您需要在 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>

In the layout files you can call to the colors or styles:

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

android:textColor="@color/titlecolor"

Checkout some examples:

查看一些示例:

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

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

回答by Randroid

You can use

您可以使用

  textView1.setTextColor(getResources().getColor(R.color.mycolor))

or

或者

  textview1.setBackgroundColor(Color.parseColor("#ffffff"));

or

或者

    textview1.setBackgroundColor(Color.RED);

or

或者

    textView1.setBackgroundColor(R.color.black);

回答by CONvid19

This may be easier:

这可能更容易:

TextView textresult = (TextView)findViewById(R.id.textView1);
textresult.setTextColor(Color.RED);

回答by Ankitkumar Makwana

try set color like this may helps you

试试这样设置颜色可能对你有帮助

txt.setTextColor(Color.rgb(0, 87, 48));

this is different way but it can change color , here need Red,Green,Blue Code to pass

这是不同的方式,但它可以改变颜色,这里需要红色,绿色,蓝色代码才能通过

回答by Sakknekedro

I am basically just merging all the partially good answers.

我基本上只是合并所有部分好的答案。

You defined your color as a String, but AFAIK Android processes colors as Itegers.
So use the Colors.xmlfile (instead of strings.xml): and refer to it in code as R.color.CodeColor.
(Moreover, I think, there is some naming convention that tells you to name these values all lowercase: code_coloror codecolor)

您将颜色定义为String,但 AFAIK Android 将颜色处理为Itegers
因此,请使用Colors.xml文件(而不是strings.xml): 并在代码中将其引用为R.color.CodeColor.
(此外,我认为,有一些命名约定会告诉您将这些值全部命名为小写:code_colorcodecolor

Or you can define them as strings, but then you are need to make it an Integer: Color.parseColor(R.string.code_color).

或者您可以将它们定义为字符串,但是您需要将其设置为 Integer: Color.parseColor(R.string.code_color)

回答by Ram kiran

you should use R.color.CodeColor. you are using R.string.CodeColor.

你应该使用R.color.CodeColor. 你正在使用R.string.CodeColor.

回答by JunR

Define colors in colors.xmlfile like that:

colors.xml文件中定义颜色,如下所示:

  <resources>
       <color name="CodeColor" >#0000ff</color>
  </resources>

Then use color whatever you like in your code using: R.color.CodeColor

然后在代码中使用任何你喜欢的颜色:R.color.CodeColor

Good luck!

祝你好运!

回答by neha

I tried something like:

我试过类似的东西:

textView.setTextColor(R.color.Red);

textView.setTextColor(R.color.Red);