I'd like to use a color from an hexa string such as "#FFFF0000"
to (say) change the background color of a Layout. Color.HSVToColor
looks like a winner but it takes a float[]
as a parameter.
我想使用一个来自六边形字符串的颜色,比如“# ff0000”来改变布局的背景颜色。颜色。HSVToColor看起来是一个赢家,但是它使用一个float[]作为参数。
Am I any close to the solution at all?
我接近解决方案了吗?
11 个解决方案
#1
477
Try Color
class method:
试试颜色类方法:
public static int parseColor (String colorString)
From Android documentation:
从Android文档:
Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
支持的格式有:#RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
#2
189
Try:
试一试:
myLayout.setBackgroundColor(Color.parseColor("#636161"));
#3
20
Convert that string to an int
color which can be used in setBackgroundColor
and setTextColor
将该字符串转换为可用于setBackgroundColor和setTextColor的int颜色
String string = "#FFFF0000";
int color = Integer.parseInt(string.replaceFirst("^#",""), 16);
The 16 means it is hexadecimal and not your regular 0-9. The result should be the same as
16代表十六进制而不是0-9。结果应该是相同的
int color = 0xFFFF0000;
#4
14
This question comes up for a number of searches related to hex color so I will add a summary here.
这个问题涉及到一些与十六进制颜色相关的搜索,所以我将在这里添加一个摘要。
Color from int
Hex colors take the form RRGGBB
or AARRGGBB
(alpha, red, green, blue). In my experience, when using an int
directly, you need to use the full AARRGGBB
form. If you only have the RRGGBB
form then just prefix it with FF
to make the alpha (transparency) fully opaque. Here is how you would set it in code. Using 0x
at the beginning means it is hexadecimal and not base 10.
十六进制颜色的形式是RRGGBB或AARRGGBB(阿尔法,红色,绿色,蓝色)。根据我的经验,当直接使用int时,您需要使用完整的AARRGGBB表单。如果您只有RRGGBB表单,那么只需在它前面加上FF,使alpha(透明)完全不透明。下面是如何在代码中设置它的方法。在开头使用0x意味着它是十六进制而不是以10为基数。
int myColor = 0xFF3F51B5;
myView.setBackgroundColor(myColor);
Color from String
As others have noted, you can use Color.parseString
like so
正如其他人所说,你可以使用颜色。parseString一样
int myColor = Color.parseColor("#3F51B5");
myView.setBackgroundColor(myColor);
Note that the String must start with a #
. Both RRGGBB
and AARRGGBB
formats are supported.
注意,字符串必须以#开头。RRGGBB和AARRGGBB格式都得到了支持。
Color from XML
You should actually be getting your colors from XML whenever possible. This is the recommended option because it makes it much easier to make color changes to your app. If you set a lot of hex colors throughout your code then it is a big pain to try to change them later.
实际上,您应该尽可能从XML获取颜色。这是推荐的选项,因为它可以更容易地对应用程序进行颜色更改。
Android material design has color palates with the hex values already configured.
Android材质设计具有与已经配置的十六进制值相匹配的颜色。
- Material Design color styling
- 材料设计颜色样式
- Material Design color palette generator
- 材料设计调色板发生器
These theme colors are used throughout your app and look like this:
这些主题颜色在你的应用中随处可见,如下图所示:
colors.xml
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#FF4081</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
If you need additional colors, a good practice to follow is to define your color in two steps in xml. First name the the hex value color and then name a component of your app that should get a certain color. This makes it easy to adjust the colors later. Again, this is in colors.xml.
如果您需要其他颜色,一个很好的实践是在xml中分两步定义颜色。首先命名十六进制值颜色,然后命名应用程序的一个组件,该组件应该具有特定的颜色。这使得以后很容易调整颜色。同样,这是在colors.xml中。
<color name="orange">#fff3632b</color>
<color name="my_view_background_color">@color/orange</color>
Then when you want to set the color in code, do the following:
然后,当您想要在代码中设置颜色时,请执行以下操作:
int myColor = ContextCompat.getColor(context, R.color.my_view_background_color);
myView.setBackgroundColor(myColor);
Android Predefined colors
The Color class comes with a number of predefined color constants. You can use it like this.
颜色类带有一些预定义的颜色常量。你可以这样使用它。
int myColor = Color.BLUE;
myView.setBackgroundColor(myColor);
Other colors are
其他颜色
Color.BLACK
- Color.BLACK
Color.BLUE
- Color.BLUE
Color.CYAN
- Color.CYAN
Color.DKGRAY
- Color.DKGRAY
Color.GRAY
- Color.GRAY
Color.GREEN
- Color.GREEN
Color.LTGRAY
- Color.LTGRAY
Color.MAGENTA
- Color.MAGENTA
Color.RED
- Color.RED
Color.TRANSPARENT
- Color.TRANSPARENT
Color.WHITE
- Color.WHITE
Color.YELLOW
- Color.YELLOW
Notes
- A quick way to find hex colors is to open the color chooser dialog in Gimp (or some other photo editing software).
- 找到十六进制颜色的快速方法是在Gimp(或其他一些照片编辑软件)中打开颜色选择器对话框。
- Standard opacity levels in Material Design
- 材料设计中的标准不透明度等级
#5
13
It's
这是
int color = Color.parseColor("colorstring");
#6
9
I use this and it works great for me for setting any color I want.
我用这个,它对我来说很适合用来设置任何我想要的颜色。
public static final int MY_COLOR = Color.rgb(255, 102, 153);
Set the colors using 0-255 for each red, green and blue then anywhere you want that color used just put MY_COLOR instead of Color.BLUE or Color.RED or any of the other static colors the Color class offers.
为每个红色、绿色和蓝色设置0-255的颜色,然后在任何您想要使用的颜色中只使用MY_COLOR而不是颜色。蓝色或颜色。红色或颜色类提供的任何其他静态颜色。
Just do a Google search for color chart and it you can find a chart with the correct RGB codes using 0-255.
只需对颜色表进行谷歌搜索,就可以找到使用0-255的正确RGB代码的图表。
#7
9
Try this:
试试这个:
vi.setBackgroundColor(Color.parseColor("#FFFF0000"));
#8
5
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"/>
#9
4
Try using 0xFFF000 instead and pass that into the Color.HSVToColor method.
尝试使用0xFFF000,并将其传递到颜色中。HSVToColor方法。
#10
3
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
#11
1
In Xamarin Use this
Xamarin的使用这
Control.SetBackgroundColor(global::Android.Graphics.Color.ParseColor("#F5F1F1"));
#1
477
Try Color
class method:
试试颜色类方法:
public static int parseColor (String colorString)
From Android documentation:
从Android文档:
Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
支持的格式有:#RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
#2
189
Try:
试一试:
myLayout.setBackgroundColor(Color.parseColor("#636161"));
#3
20
Convert that string to an int
color which can be used in setBackgroundColor
and setTextColor
将该字符串转换为可用于setBackgroundColor和setTextColor的int颜色
String string = "#FFFF0000";
int color = Integer.parseInt(string.replaceFirst("^#",""), 16);
The 16 means it is hexadecimal and not your regular 0-9. The result should be the same as
16代表十六进制而不是0-9。结果应该是相同的
int color = 0xFFFF0000;
#4
14
This question comes up for a number of searches related to hex color so I will add a summary here.
这个问题涉及到一些与十六进制颜色相关的搜索,所以我将在这里添加一个摘要。
Color from int
Hex colors take the form RRGGBB
or AARRGGBB
(alpha, red, green, blue). In my experience, when using an int
directly, you need to use the full AARRGGBB
form. If you only have the RRGGBB
form then just prefix it with FF
to make the alpha (transparency) fully opaque. Here is how you would set it in code. Using 0x
at the beginning means it is hexadecimal and not base 10.
十六进制颜色的形式是RRGGBB或AARRGGBB(阿尔法,红色,绿色,蓝色)。根据我的经验,当直接使用int时,您需要使用完整的AARRGGBB表单。如果您只有RRGGBB表单,那么只需在它前面加上FF,使alpha(透明)完全不透明。下面是如何在代码中设置它的方法。在开头使用0x意味着它是十六进制而不是以10为基数。
int myColor = 0xFF3F51B5;
myView.setBackgroundColor(myColor);
Color from String
As others have noted, you can use Color.parseString
like so
正如其他人所说,你可以使用颜色。parseString一样
int myColor = Color.parseColor("#3F51B5");
myView.setBackgroundColor(myColor);
Note that the String must start with a #
. Both RRGGBB
and AARRGGBB
formats are supported.
注意,字符串必须以#开头。RRGGBB和AARRGGBB格式都得到了支持。
Color from XML
You should actually be getting your colors from XML whenever possible. This is the recommended option because it makes it much easier to make color changes to your app. If you set a lot of hex colors throughout your code then it is a big pain to try to change them later.
实际上,您应该尽可能从XML获取颜色。这是推荐的选项,因为它可以更容易地对应用程序进行颜色更改。
Android material design has color palates with the hex values already configured.
Android材质设计具有与已经配置的十六进制值相匹配的颜色。
- Material Design color styling
- 材料设计颜色样式
- Material Design color palette generator
- 材料设计调色板发生器
These theme colors are used throughout your app and look like this:
这些主题颜色在你的应用中随处可见,如下图所示:
colors.xml
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#FF4081</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
If you need additional colors, a good practice to follow is to define your color in two steps in xml. First name the the hex value color and then name a component of your app that should get a certain color. This makes it easy to adjust the colors later. Again, this is in colors.xml.
如果您需要其他颜色,一个很好的实践是在xml中分两步定义颜色。首先命名十六进制值颜色,然后命名应用程序的一个组件,该组件应该具有特定的颜色。这使得以后很容易调整颜色。同样,这是在colors.xml中。
<color name="orange">#fff3632b</color>
<color name="my_view_background_color">@color/orange</color>
Then when you want to set the color in code, do the following:
然后,当您想要在代码中设置颜色时,请执行以下操作:
int myColor = ContextCompat.getColor(context, R.color.my_view_background_color);
myView.setBackgroundColor(myColor);
Android Predefined colors
The Color class comes with a number of predefined color constants. You can use it like this.
颜色类带有一些预定义的颜色常量。你可以这样使用它。
int myColor = Color.BLUE;
myView.setBackgroundColor(myColor);
Other colors are
其他颜色
Color.BLACK
- Color.BLACK
Color.BLUE
- Color.BLUE
Color.CYAN
- Color.CYAN
Color.DKGRAY
- Color.DKGRAY
Color.GRAY
- Color.GRAY
Color.GREEN
- Color.GREEN
Color.LTGRAY
- Color.LTGRAY
Color.MAGENTA
- Color.MAGENTA
Color.RED
- Color.RED
Color.TRANSPARENT
- Color.TRANSPARENT
Color.WHITE
- Color.WHITE
Color.YELLOW
- Color.YELLOW
Notes
- A quick way to find hex colors is to open the color chooser dialog in Gimp (or some other photo editing software).
- 找到十六进制颜色的快速方法是在Gimp(或其他一些照片编辑软件)中打开颜色选择器对话框。
- Standard opacity levels in Material Design
- 材料设计中的标准不透明度等级
#5
13
It's
这是
int color = Color.parseColor("colorstring");
#6
9
I use this and it works great for me for setting any color I want.
我用这个,它对我来说很适合用来设置任何我想要的颜色。
public static final int MY_COLOR = Color.rgb(255, 102, 153);
Set the colors using 0-255 for each red, green and blue then anywhere you want that color used just put MY_COLOR instead of Color.BLUE or Color.RED or any of the other static colors the Color class offers.
为每个红色、绿色和蓝色设置0-255的颜色,然后在任何您想要使用的颜色中只使用MY_COLOR而不是颜色。蓝色或颜色。红色或颜色类提供的任何其他静态颜色。
Just do a Google search for color chart and it you can find a chart with the correct RGB codes using 0-255.
只需对颜色表进行谷歌搜索,就可以找到使用0-255的正确RGB代码的图表。
#7
9
Try this:
试试这个:
vi.setBackgroundColor(Color.parseColor("#FFFF0000"));
#8
5
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"/>
#9
4
Try using 0xFFF000 instead and pass that into the Color.HSVToColor method.
尝试使用0xFFF000,并将其传递到颜色中。HSVToColor方法。
#10
3
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
#11
1
In Xamarin Use this
Xamarin的使用这
Control.SetBackgroundColor(global::Android.Graphics.Color.ParseColor("#F5F1F1"));