【问题标题】:Android Styles heritageAndroid 风格传承
【发布时间】:2016-06-29 02:37:35
【问题描述】:

这是交易:在我的 Styles.xml 中,我有一个通用样式,一个继承另一个,如下所示:

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar" parent="@style/AppTheme">
    <item name="colorPrimary">#0acf66</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

当我应用第二个主题 (AppTheme.NoActionBar) 时,它会继承颜色,但操作栏仍然存在。如果我将第二种样式的父级更改为 Theme.AppCompat.NoActionBar 它可以工作,但颜色完全不同。我该如何解决?

【问题讨论】:

    标签: android


    【解决方案1】:

    所以这是样式/主题的继承问题。从样式继承属性有两种方法。

    1.通过点表示法(隐式):

    <style name="Theme.AppCompat.NoActionBar.Custom">
      // the parent will be Theme.AppCompat.NoActionBar
    </style>
    

    2.通过父标签(显式)

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
       //the parent is Theme.AppCompat.Light.DarkActionBar
    </style>
    

    要始终记住的一件事是,显式继承胜过隐式继承。

    所以你的情况

    <style name="AppTheme.NoActionBar" parent="@style/AppTheme">
        // your parent is AppTheme, and new style is AppThem.NoActionBar,
           but it doesn't inherit from an actual NoActionBar style.
    </style>
    

    为了有一个“NoActionBar”主题,实际上你必须继承一个具有该属性的样式。因此,您可以创建另一个继承自 Theme.AppCompat.NoActionBarAppTheme.NoActionBar

    要更好地了解主题/样式,请查看这个很棒的Google I/O 2016 链接,了解样式和主题。

    【讨论】:

    • 感谢您的回复。我只是想避免在两个主题中重复像 #0acf66 这样的项目。有没有办法做到这一点并且仍然在第二个主题中隐藏动作栏?
    • 据我所知,没有。但通常的做法是有两个具有相同属性的主题,一个有操作栏,一个没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2014-12-25
    • 2013-07-11
    • 2013-04-30
    • 2020-01-17
    • 2011-01-24
    相关资源
    最近更新 更多