Android样式继承 [英] Android style inheritance

查看:60
本文介绍了Android样式继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主题,一个有ActionBar,一个没有.重复样式似乎多余,有什么方法可以简化吗?

I have two themes, one has ActionBar and one without. It seems redundant to do duplicate the styles, is there any way to simplify it?

谢谢

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_color</item>
    <item name="android:windowBackground">@color/bg_color</item>
    <item name="colorAccent">@color/accent_color</item>
    <item name="colorPrimaryDark">@color/darker_color</item>

    <!-- Button style -->
    <item name="android:buttonStyle">@style/ButtonStyle</item>
    <item name="buttonStyle">@style/ButtonStyle</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary_color</item>
    <item name="android:windowBackground">@color/bg_color</item>
    <item name="colorAccent">@color/accent_color</item>
    <item name="colorPrimaryDark">@color/darker_color</item>

    <!-- Button style -->
    <item name="android:buttonStyle">@style/ButtonStyle</item>
    <item name="buttonStyle">@style/ButtonStyle</item>
</style>

<style name="ButtonStyle" parent="Widget.AppCompat.Button">
    <item name="android:background">@color/primary_color</item>
</style>

推荐答案

不幸的是,由于样式/主题继承的工作方式,因此无法解决此问题.您可以在这里阅读更多有关此内容的信息:

Unfortunately, due to the way that Style/Theme inheritance works, there is no way around this. You could read more about that here:

Android样式的遗产

但是,一个选项是复制NoActionBar主题的内容.事实证明,它仅包含2行:

One option, however, would be to copy the contents of the NoActionBar theme. It turns out that it only contains 2 lines:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

所以您的代码看起来像这样

So your code would look like this

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary_color</item>
    <item name="android:windowBackground">@color/bg_color</item>
    <item name="colorAccent">@color/accent_color</item>
    <item name="colorPrimaryDark">@color/darker_color</item>

    <!-- Button style -->
    <item name="android:buttonStyle">@style/ButtonStyle</item>
    <item name="buttonStyle">@style/ButtonStyle</item>
</style>

<!-- NoActionBar theme -->
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

现在,您将继承AppTheme属性并获得NoActionBar行为.显然,如果将来再更改NoActionBar的属性,这并不是万无一失的,但是对于在其基本AppTheme中具有许多属性的人来说,这可能是一个不错的选择.

Now you are inheriting the AppTheme attributes as well as getting the NoActionBar behavior. Obviously this is not foolproof if the attributes of NoActionBar change in the future, but it might be a good option for someone with many attributes in their base AppTheme.

这篇关于Android样式继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆