java Android:从 XML 颜色常量以编程方式设置颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29815538/
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: set color programatically from XML color constants
提问by prototype0815
Trying to set a color which is defined in res/values/colors.xml to an object,
尝试将 res/values/colors.xml 中定义的颜色设置为对象,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="listViewSelected">@android:color/holo_blue_light</drawable>
<drawable name="listViewPressed">@android:color/holo_green_light</drawable>
<drawable name="pagerTabStrip">#2B3333</drawable>
<!--<drawable name="pagerTabStrip">#353F3E</drawable>-->
<drawable name="tableHead">#FF444444</drawable>
</resources>
I can not figure out why it is not working,
I tried a lot of approaches (getResources(), Color.parseColor(), ...)
我不知道为什么它不起作用,我尝试了很多方法 (getResources(), Color.parseColor(), ...)
How do I set the color "tableHead" e.g. to a TextView?
如何将颜色“tableHead”设置为 TextView?
tv.setBackgroundColor(????);
tv.setBackgroundColor(????);
回答by Sohaib
Color entries should be like this
颜色条目应该是这样的
<color name="tableHead">#FF444444</color>
<color name="tableHead">#FF444444</color>
and use tv.setBackgroundResource(R.color.tableHead);
并使用 tv.setBackgroundResource(R.color.tableHead);
回答by Jain Nidhi
Use,..
利用,..
Color.parseColor("#bdbdbd");
like,
喜欢,
mTextView.setTextColor(Color.parseColor("#bdbdbd"));
OR......................
或者......................
Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.
获取使用的根布局的句柄,然后在其上设置背景颜色。根布局是您使用 setContentView 调用的任何内容。
// Now get a handle to any View contained // within the main layout you are using
// 现在获取包含在您正在使用的主布局中的任何视图的句柄 //
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
// 找到根视图
View root = someView.getRootView()
// Set the color
// 设置颜色
root.setBackgroundColor(getResources().getColor(android.R.color.red));
回答by Neal Ahluvalia
tv.setTextColor(getResources().getColor(R.color.tableHead));
And guess what your colors.xml should be like this
猜猜你的colors.xml应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tableHead">#FF444444</color>
</resources>
回答by A.R.
Your color.xml should look like this:
您的 color.xml 应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tableHead">#FF444444</color>
</resources>
How you will use this color to set in textview: Like this
您将如何使用此颜色在 textview 中进行设置:像这样
tv.setBackgroundColor(getResources().getColor(R.color.tableHead));
回答by Krupa Patel
Try something like this:
尝试这样的事情:
tv.setBackgroundResource(Color.parseColor("#ffffff"));
回答by Rajen Raiyarela
Firstly modify your color.xml as below
首先修改你的 color.xml 如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="listViewSelected">@android:color/holo_blue_light</drawable>
<color name="listViewPressed">@android:color/holo_green_light</drawable>
<color name="pagerTabStrip">#2B3333</drawable>
<!--<color name="pagerTabStrip">#353F3E</drawable>-->
<color name="tableHead">#FF444444</drawable>
</resources>
For setting the textview background color you can do like
要设置 textview 背景颜色,您可以这样做
tv.setBackgroundColor(R.color.tableHead);
Additionally for setting the textview textcolor you can do like
另外为了设置 textview textcolor 你可以这样做
tv_empty.setTextColor(R.color.tableHead)