【发布时间】: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