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

非常感谢你! –