用十六进制更改背景颜色

时间:2015-12-28 19:51:46

标签: android android-activity colors hex

在mainActivity中我使用hexString然后启动新活动并向其发送hexString 并使用此十六进制字符串更改背景颜色。 我需要知道是否有背景颜色方法可以采用参数hexString或long。

代码:

String colorValue = getIntent().getStringExtra("colorHex");
findViewById(R.id.layout1).setBackgroundColor(Color.parseColor(colorValue));//want change in the argument or if there's another method.

2 个答案:

答案 0 :(得分:1)

Google Documentation

尝试使用public static int parseColor (String colorString)功能 取一个String并返回int颜色!

  

解析颜色字符串,并返回相应的color-int。如果无法解析字符串,则抛出IllegalArgumentException异常。支持的格式为:#RRGGBB #AARRGGBB或以下名称之一:'red','blue','green','black','white','grey','cyan','magenta','yellow' ,'lightgray','darkgray','grey','lightgrey','darkgrey','aqua','fuchsia','lime','maroon','navy','olive','purple','银','teal'。

尝试类似这样的内容

//find your layout
LinearLayout ll = (LinearLayout) findViewById(R.id.yourLinearLayout);

//Get the color from an EditText
EditText newcolor = (EditText) findViewById(R.id.yourEditText);
String stringColor = newcolor.getText().toString(): //Assume that you have the #RRGGBB

//the function take only #RRGGBB with 6 values read documentation for more information
 int intColor = Color.parseColor(stringColor);

//Set the color to the LinearLayout
ll.setBackground(intColor); 

答案 1 :(得分:0)

假设颜色为黄色。

{{1}}
相关问题