我想爲我的佈局設置背景顏色。 問題是它給了我:第二個參數類型錯誤
錯誤的第二個參數類型。需要int。 我只是改變R.color.red
到color variable
//add background color header
String color = pref.getString("color", null);
if(color != null) {
color = "R.color." + color;
LinearLayout rl = (LinearLayout) findViewById(R.id.menuHeader);
rl.setBackgroundColor(ContextCompat.getColor(this, color));
}
有什麼不好?怎麼解決?
color ir是一個我在sharedpreferences中獲得的變量。它是紅色,藍色......但設置顏色我需要它在style.xml'R.color.red'中的引用問題是如果我把R.color.red它的作品,但我需要在那裏通過一個動態顏色。 –
對不起,我的錯。我添加了一個答案,讓我知道這是否適合你。 – mt0s