1

我有一個活動,我想根據用戶偏好更改顏色。如何根據用戶偏好更改RelativeLayout顏色?

我在styles.xml創造了許多風格

<style name="Yellow" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">$ffff00</item> 
    <item name="colorPrimaryDark">#000000</item> 
    <item name="colorAccent">#ffff00</item> 
</style> 

<style name="Red" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">#ff0000</item> 
    <item name="colorPrimaryDark">#000000</item> 
    <item name="colorAccent">#ff0000</item> 
</style> 

這是確定了我的工具欄,等等。問題是,我想改變一個RelativeLayout的背景選擇colorPrimary顏色。它獲取我的默認AppTheme顏色。

我活動的OnCreate:

setTheme(R.style.Yellow); 

myactivity.xml

<RelativeLayout 
      android:orientation="vertical" android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:background="@color/colorPrimary"> 

如何設置myactivity基於黃色或紅色的RelativeLayout bkcolor(用戶任選其一)?

回答

1

在color.xml資源

<color name="red">#FF0000</color> 

,並設置定義顏色它來查看背景

RelativeLayout rl = (RelativeLayout)findViewById(R.id.rlid); 
rl.setBackgroundColor(R.color.read) 

或創建的顏色選擇like this

<item name="colorPrimary">@color/red</item> 
+0

非常感謝你! –