android TextView:动态设置背景颜色不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1466788/
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
android TextView: setting the background color dynamically doesn't work
提问by Tawani
Setting the background color programatically of an android TextView
doesn't seem to work.
I'm I missing something!
以编程方式设置 android 的背景颜色TextView
似乎不起作用。我错过了什么!
TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(R.color.white);
I also have this file (colors.xml) in my res/values folder
我的 res/values 文件夹中也有这个文件 (colors.xml)
<resources>
<color name="white">#ffffffff</color>
<color name="black">#ff000000</color>
</resources>
[EDIT]: Also, setting the text color causes the TextView to disappear.
[编辑]:此外,设置文本颜色会导致 TextView 消失。
TextView c1 = new TextView(activity);
c1.setTextColor(R.color.solid_red);
c1.setText("My Text");
回答by bhatt4982
Use et.setBackgroundResource(R.color.white);
用 et.setBackgroundResource(R.color.white);
回答by Seb DA ROCHA
Try this:
尝试这个:
TextView c1 = new TextView(activity);
c1.setTextColor(getResources().getColor(R.color.solid_red));
c1.setText("My Text");
I agree that a color and a resource have the same type, but I also spend a few hours to find this solution.
我同意颜色和资源具有相同的类型,但我也花了几个小时来找到这个解决方案。
回答by Dmitry
To set red color:
设置红色:
textView.setBackgroundColor(0xfff00000);
Or
或者
<color name="solid_red">#fff00000</color>
textView.setBackgroundResource(R.color.solid_red);
回答by badMonkey
I had a similar issue where I was creating a numeric color without considering the leading alpha channel. ie. mytext.setTextColor(0xFF0000)
(thinking this would be red ). While this is a red color it is also 100% transparent as it = 0x00FF0000;
The correct 100% opaque value is 0xFFFF0000
or mytext.setTextcolor(0xFFFF0000)
.
我有一个类似的问题,我在不考虑领先的 alpha 通道的情况下创建数字颜色。IE。 mytext.setTextColor(0xFF0000)
(认为这会是红色的)。虽然这是一种红色,但它也是 100% 透明的,因为it = 0x00FF0000;
正确的 100% 不透明值为0xFFFF0000
或mytext.setTextcolor(0xFFFF0000)
。
回答by Vivek Warde
Just this 1 line of code changed the background programmatically
仅这 1 行代码就以编程方式更改了背景
tv.setBackgroundColor(Color.parseColor("#808080"));
回答by user1252459
Well I had situation when web service returned a color in hex format like "#CC2233" and I wanted to put this color on textView by using setBackGroundColor(), so I used android Color class to get int value of hex string and passed it to mentioned function. Everything worked. This is example:
好吧,我遇到了 Web 服务以“#CC2233”等十六进制格式返回颜色的情况,我想通过使用 setBackGroundColor() 将此颜色放在 textView 上,因此我使用 android Color 类获取十六进制字符串的 int 值并将其传递给提到的功能。一切正常。这是示例:
String myHexColor = "#CC2233";
TextView myView = (TextView) findViewById(R.id.myTextView);
myView.setBackGroundColor(Color.pasrsehexString(myHexColor));
P.S. posted this answer because other solutions didn't work for me. I hope this will help someone:)
PS发布此答案是因为其他解决方案对我不起作用。我希望这会帮助某人:)
回答by dharmx
here is in little detail,
这里有一点细节,
if you are in activity use this
如果你在活动中使用这个
textview.setBackground(ContextCompat.getColor(this,R.color.yourcolor));
if you are in fragment use below code
如果你在片段中使用下面的代码
textview.setBackground(ContextCompat.getColor(getActivity(),R.color.yourcolor));
if you are in recyclerview adapter use below code
如果您在 recyclerview 适配器中,请使用以下代码
textview.setBackground(ContextCompat.getColor(context,R.color.yourcolor));
// use holder.textview if you are in onBindviewholder
//here context is passed from fragment
回答by Sahil Sharma
Here are the steps to do it correctly:
以下是正确执行此操作的步骤:
First of all, declare an instance of TextView in your MainActivity.java as follows:
TextView mTextView;
Set some text DYNAMICALLY(if you want) as follows:
mTextView.setText("some_text");
Now, to set the background color, you need to define your own color in the res->values->colors.xml file as follows:
<resources> <color name="my_color">#000000</color> </resources>
You can now use "my_color" color in your java file to set the background dynamically as follows:
mTextView.setBackgroundResource(R.color.my_color);
首先,在 MainActivity.java 中声明一个 TextView 实例,如下所示:
TextView mTextView;
动态设置一些文本(如果需要)如下:
mTextView.setText("some_text");
现在,要设置背景颜色,您需要在 res->values->colors.xml 文件中定义自己的颜色,如下所示:
<resources> <color name="my_color">#000000</color> </resources>
您现在可以在 java 文件中使用“my_color”颜色来动态设置背景,如下所示:
mTextView.setBackgroundResource(R.color.my_color);
回答by Yash Patil
tv.setTextColor(getResources().getColor(R.color.solid_red));
回答by Siddharth
Color.parseHexColor("17ee27")
did not work for me, instead Color.parseColor("17ee27")
worked perfectly.
Color.parseHexColor("17ee27")
对我不起作用,而是Color.parseColor("17ee27")
完美地工作。