Android 如何在动态布局中动态传递颜色?

Android 如何在动态布局中动态传递颜色?,android,background-color,Android,Background Color,我正在使用以下代码创建动态布局 LinearLayout outerLinearLayout = new LinearLayout(activity); outerLinearLayout.setBackgroundColor(Color.WHITE); 正如您所看到的,我正在将其背景色设置为白色,现在假设我有一个字符串数组,其中包含类似{“#FF2233”,…}的元素,那么如何动态地将这个十六进制值传递给setBackgroundColor() 我的动机是在运行时传递动态颜色值,而不是传递固

我正在使用以下代码创建动态布局

LinearLayout outerLinearLayout = new LinearLayout(activity);
outerLinearLayout.setBackgroundColor(Color.WHITE);
正如您所看到的,我正在将其背景色设置为白色,现在假设我有一个字符串数组,其中包含类似{“#FF2233”,…}的元素,那么如何动态地将这个十六进制值传递给
setBackgroundColor()


我的动机是在运行时传递动态颜色值,而不是传递固定值,如
color.WHITE

尝试使用color类,该类有一种方法将十六进制颜色解析为Android有效值

Color.parseColor("#FF2233");

颜色的值是整数,因此可以将其用作字符串,然后再转换为int

int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
并根据您的需要使用此颜色

或者你可以直接喜欢
Color.parseColor(“你的颜色值”)

您可以将十六进制字符串值解析为颜色,并使用

int color=Integer.parseInt(“bdbdbd”,16)+0xFF000000; 作为

或者使用Color.parseColor(“您的颜色值”) 作为


这很简单。您只需将您的
十六进制字符串
解析为
颜色
并加载如下内容:

 LinearLayout outerLinearLayout = new LinearLayout(activity);

 outerLinearLayout.setBackgroundColor(Color.parseColor("#101010"));

Color.parseColor(#“+您的颜色值)
通过了

非常好,这就是我要找的东西。我没有提到它是动态布局。答案已经给出了,请看一看。希望能帮助你!!(+1)对于这个问题,有趣的是,我不知道你的代码是如何工作的,你能解释一下吗?嗨,你可以把你的十六进制字符串解析成颜色,然后像wither int Color=Integer.parseInt(“bdbdbd”,16)+0xFF000000;或Color.parseColor(“您的颜色值”)作为LinearLayout outerLinearLayout=新的LinearLayout(活动);setBackgroundColor(Color.parseColor(“您的颜色值”));哦,您正在将字符串转换为十六进制ising Integer.parInt()。我怎么忘了这个。这也是一种很好的方式。谢谢你的回答。
 LinearLayout outerLinearLayout = new LinearLayout(activity); outerLinearLayout.setBackgroundColor(Color.parseColor("yours color value"))
 LinearLayout outerLinearLayout = new LinearLayout(activity);

 outerLinearLayout.setBackgroundColor(Color.parseColor("#101010"));