【问题标题】:how to change a RelativeLayout color based on user preference?如何根据用户偏好更改 RelativeLayout 颜色?
【发布时间】:2017-02-28 22:36:13
【问题描述】:

我有一个要根据用户偏好更改颜色的活动。

我在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>

我的工具栏等没问题...问题是我想用选定的 colorPrimary 颜色更改 RelativeLayout 背景。它正在获取我的默认 AppTheme 颜色。

创建我的活动:

setTheme(R.style.Yellow);

我的活动.xml

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

如何根据黄色或红色设置myactivity relativelayout bkcolor(用户将选择其中一个)?

【问题讨论】:

    标签: android android-layout android-studio android-xml android-theme


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      相关资源
      最近更新 更多