【问题标题】:Change only color when changing Theme更改主题时仅更改颜色
【发布时间】:2017-07-04 08:29:26
【问题描述】:

我想在我的 android 应用程序中支持不同的主题。 主题应该只有两种颜色不同。所以每个主题都有这些颜色:

  • 原色
  • 二次色

我想要的是,有一个大的默认主题,也有像按钮样式等子项,像这样 (styles.xml):

<style name="DefaultTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="homeAsUpIndicator">@drawable/my_stackoverflow_arrow</item>
    [...]
</style>

<style name="DefaultTheme.NoActionBar">
    <!-- hide action bar and title -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="DefaultTheme.Button">
    <item name="android:textAllCaps">true</item>
    <item name="android:textStyle">bold</item>
    [...]
</style>

但现在我也想改变不同颜色的主题。因此,我在attrs.xml文件中创建了一个资源:

<resources>
    <attr name="ThemeColorPrimary" format="reference" />
    <attr name="ThemeColorSecondary" format="reference" />
</resources>

所以现在我可以像这样在我的styles.xml 中使用这个属性:

<style name="DefaultTheme.Button">
    <item name="android:textAllCaps">true</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?attr/ThemeColorPrimary</item>
    [...]
</style>

但是如何创建我的彩色主题?我知道我需要这样做:

<item name="ThemeColorPrimary">@color/ThemeBluePrimary</item>

但是在哪里?哪个主题继承自哪个?如果我现在有类似的东西:

<style name="BlueTheme" parent="DefaultTheme">
    <item name="ThemeColorPrimary">@color/ThemeBluePrimary</item>
    <item name="ThemeColorSecondary">@color/ThemeBlueSecondary</item>
</style>

<style name="GreyTheme" parent="DefaultTheme">
    <item name="ThemeColorPrimary">@color/ThemeGreyPrimary</item>
    <item name="ThemeColorSecondary">@color/ThemeGreySecondary</item>
</style>

<style name="BlackTheme" parent="DefaultTheme">
    <item name="ThemeColorPrimary">@color/ThemeBlackPrimary</item>
    <item name="ThemeColorSecondary">@color/ThemeBlackSecondary</item>
</style>

我无法在AndroidManifest.xml 中指定这样的主题:

<activity
    android:name="StackOverflowActivity"
    android:screenOrientation="portrait"
    android:theme="@style/DefaultTheme.NoActionBar" />

因为指定的资源会丢失。否则我也不能这样做:

<activity
    android:name="StackOverflowActivity"
    android:screenOrientation="portrait"
    android:theme="@style/BlueTheme.NoActionBar" />

因为 BlueTheme 没有名为“NoActionBar”的 Child。

我知道这很令人困惑,我自己也很困惑。 我该如何解决这个问题? 提前谢谢你。

【问题讨论】:

    标签: android android-resources android-theme android-styles android-color


    【解决方案1】:

    您必须在父主题中指定那些自定义属性:

    <style name="DefaultTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="ThemeColorPrimary">@color/defaultPrimaryColor</item>
        <item name="ThemeColorSecondary">@color/defaultSecondaryColor</item>
    </style>
    

    然后DefaultTheme 的每个孩子都可以访问您的自定义属性,也可以覆盖这些属性。

    如果你想结合NoActionBarBlueTheme,那么你必须继承NoActionBar主题:

    <style name="BlueNoActionBarTheme" parent="DefaultTheme.NoActionBar">
        <item name="ThemeColorPrimary">@color/ThemeBluePrimary</item>
        <item name="ThemeColorSecondary">@color/ThemeBlueSecondary</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多