从Dark and Light主题中提取常用属性

时间:2016-01-12 07:04:24

标签: android android-theme android-styles

我的应用程序中有一个黑暗主题和一个浅色主题,黑暗主题扩展了Theme.AppCompat.NoActionBar,灯光主题扩展了Theme.AppCompat.Light.NoActionBar。

我在那些明暗主题中有很多自定义属性,我必须在这两个主题中复制。

我想知道是否有办法在一个普通的地方实际拥有所有这些属性,并被明暗主题使用。

以下示例使事情更加清晰:

 <style name="MotherThemeDark" parent="Theme.AppCompat.NoActionBar">
        <!-- Base application theme common to all sdk versions -->
        <!--Colors-->
        <item name="colorPrimaryDark">@color/brand_dark</item>
        <item name="colorPrimary">@color/brand</item>
        <item name="colorAccent">@color/brand_accent</item>
        <item name="colorPositive">@color/positive</item>
        <item name="colorPositiveDark">@color/positive_dark</item>
        <item name="colorNegative">@color/negative</item>
        <item name="colorNegativeDark">@color/negative_dark</item>
        <item name="colorNegativeLight">@color/negative_light</item>
        <item name="colorMedium">@color/medium</item>
....
...Things specific to this theme goes here
....
</style>



 <style name="MotherTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Base application theme common to all sdk versions -->
            <!--Colors-->
            <item name="colorPrimaryDark">@color/brand_dark</item>
            <item name="colorPrimary">@color/brand</item>
            <item name="colorAccent">@color/brand_accent</item>
            <item name="colorPositive">@color/positive</item>
            <item name="colorPositiveDark">@color/positive_dark</item>
            <item name="colorNegative">@color/negative</item>
            <item name="colorNegativeDark">@color/negative_dark</item>
            <item name="colorNegativeLight">@color/negative_light</item>
            <item name="colorMedium">@color/medium</item>
    ....
    ...Things specific to this theme goes here
    ....
    </style>

2 个答案:

答案 0 :(得分:1)

其中一种方法是将公共属性放在主题叠加层中(假设您使用的是AppCompat库),例如: -

<style name=“MyCustomOverlay” parent=”ThemeOverlay.AppCompat”>
   <item name="colorPrimaryDark">@color/brand_dark</item>
   <item name="colorPrimary">@color/brand</item>
   <item name="colorAccent">@color/brand_accent</item>
   <item name="colorPositive">@color/positive</item>
   <item name="colorPositiveDark">@color/positive_dark</item>
   <item name="colorNegative">@color/negative</item>
   <item name="colorNegativeDark">@color/negative_dark</item>
   <item name="colorNegativeLight">@color/negative_light</item>
   <item name="colorMedium">@color/medium</item>
</style>

然后将android:theme=”MyCustomOverlay”应用于您的根视图。这将覆盖基本主题(例如MotherThemeMotherThemeDark)中为根视图和所有子视图设置的属性。适用于API 11 +。

参考:https://plus.google.com/+AndroidDevelopers/posts/JXHKyhsWHAH

答案 1 :(得分:0)

虽然我将上述帖子作为答案,但由于它帮助我找到了解决方案,我将在此处发布实际解决方案:

我将黑暗主题扩展为ThemeOverlay.AppCompat.Dark,然后将motherThemeDark应用于我希望以黑暗为主题的片段/活动的根布局。在MotherThemeDark下,我只有一些我想要覆盖黑暗主题的属性。

<style name="MotherTheme" parent="Theme.AppCompat.NoActionBar">
            <!-- Base application theme common to all sdk versions -->
            <!--Colors-->
            <item name="colorPrimaryDark">@color/brand_dark</item>
            <item name="colorPrimary">@color/brand</item>
            <item name="colorAccent">@color/brand_accent</item>
            <item name="colorPositive">@color/positive</item>
            <item name="colorPositiveDark">@color/positive_dark</item>
            <item name="colorNegative">@color/negative</item>
            <item name="colorNegativeDark">@color/negative_dark</item>
            <item name="colorNegativeLight">@color/negative_light</item>
            <item name="colorMedium">@color/medium</item>
            ....
            ...Things specific to this theme goes here
            ....
</style>



 <style name="MotherThemeDark" parent="ThemeOverlay.AppCompat.Dark">
    ....   
    ....
    ...Things specific to this theme goes here
    ....
 </style>