2015-12-28 70 views
0

在mainActivity我採取hexString然後開始新的活動,併發送hexString到它 並改變背景顏色與此十六進制字符串。 我需要知道是否有背景顏色方法可以採取參數hexString或長。改變背景顏色以十六進制

代碼:

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

回答

1

Google Documentation

嘗試使用功能public static int parseColor (String colorString) 這需要字符串,返回整型顏色!

解析顏色字符串,並返回相應的color-int。如果字符串不能被解析,則拋出一個IllegalArgumentException異常。支持的格式爲:#RRGGBB #AARRGGBB或以下名稱之一:'紅','藍','綠','黑','白','灰色','青色','洋紅色','黃色' ,'灰色','暗灰色','灰色','lightgrey','darkgrey','aqua','fuchsia','lime','栗色','藏青色','橄欖色','紫色'銀','深青色'。

嘗試是這樣的

//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); 
+0

不能工作,使新的活動停止,如果我插入六角前0xFFF的。 – Magician

+0

嘗試使用#,像這樣'public static int parseColor(「#0xfff」)' 使用try和catch,因爲**如果字符串不能被解析,拋出一個IllegalArgumentException異常** – Dario

+0

當它開始新的活動。 – Magician

0

假設黃色。

private static int yellow = Color.parseColor("#f9ce1e"); 
view.setBackgroundColor(yellow);