本文翻译自:How to get a Color from hexadecimal Color String
I'd like to use a color from an hexa string such as "#FFFF0000"
to (say) change the background color of a Layout. 我想使用六角形字符串中的颜色,例如"#FFFF0000"
来(例如)更改布局的背景颜色。 Color.HSVToColor
looks like a winner but it takes a float[]
as a parameter. Color.HSVToColor
看起来像赢家,但它需要一个float[]
作为参数。
Am I any close to the solution at all? 我是否接近解决方案?
#1楼
参考:https://stackoom.com/question/m1oz/如何从十六进制颜色字符串中获取颜色
#2楼
尝试:
myLayout.setBackgroundColor(Color.parseColor("#636161"));
#3楼
XML file saved at res/values/colors.xml: 保存在res / values / colors.xml的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
This application code retrieves the color resource: 此应用程序代码检索颜色资源:
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
This layout XML applies the color to an attribute: 此布局XML将颜色应用于属性:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/translucent_red"
android:text="Hello"/>
#4楼
If you define a color in your XML and want to use it to change background color or something this API is the one your are looking for: 如果您在XML中定义颜色并希望使用它来更改背景颜色或某些内容,则此API就是您要查找的颜色:
((TextView) view).setBackgroundResource(R.drawable.your_color_here);
In my sample I used it for TestView 在我的示例中,我将它用于TestView
#5楼
试试这个:
vi.setBackgroundColor(Color.parseColor("#FFFF0000"));
#6楼
它的
int color = Color.parseColor("colorstring");