从深色和浅色主题中提取共同属性
Extracting common attributes from a Dark and Light theme
我的应用中有一个深色主题和一个浅色主题,其中深色主题扩展 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>
其中一种方法是将公共属性放在主题覆盖中(假设您使用的是 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”
应用于您的根视图。这将覆盖根视图和所有子视图的基本主题中设置的属性(例如 MotherTheme
或 MotherThemeDark
)。适用于 API 11+。
参考:https://plus.google.com/+AndroidDevelopers/posts/JXHKyhsWHAH
尽管我将上述 post 作为答案,但由于它帮助我找到了解决方案,因此我将 post 实际解决方案放在这里:
我扩展了深色主题 ThemeOverlay.AppCompat.Dark,然后将 motherThemeDark 应用到我想设为深色主题的 fragment/activity 的根布局。在 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>
我的应用中有一个深色主题和一个浅色主题,其中深色主题扩展 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>
其中一种方法是将公共属性放在主题覆盖中(假设您使用的是 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”
应用于您的根视图。这将覆盖根视图和所有子视图的基本主题中设置的属性(例如 MotherTheme
或 MotherThemeDark
)。适用于 API 11+。
参考:https://plus.google.com/+AndroidDevelopers/posts/JXHKyhsWHAH
尽管我将上述 post 作为答案,但由于它帮助我找到了解决方案,因此我将 post 实际解决方案放在这里:
我扩展了深色主题 ThemeOverlay.AppCompat.Dark,然后将 motherThemeDark 应用到我想设为深色主题的 fragment/activity 的根布局。在 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>