如何从十六进制颜色字符串中获取颜色

本文翻译自: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");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值