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

问题描述

这很重要:在我的Styles.xml中,我有一种通用样式,一种继承了另一种样式,如下所示:

Here is the deal: In my Styles.xml I have a general style and one inherinting the other as it follows:

<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,则可以使用,但是颜色完全不同.我该如何解决?

When I apply the second theme (AppTheme.NoActionBar) it inherits the color but the action bar is still there. If i change the parent of the second style to Theme.AppCompat.NoActionBar it works, but the colors are totally diferent. How can I fix it?

推荐答案

因此,这是样式/样式的继承问题.有两种从样式继承属性的方法.

So this is an inheritance problem for styles/themse. There are two ways to inherit properties from styles.

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

1.Through dot notation (implicit):

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

2.通过父标记(显式)

2.Through parent tag (explicit)

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

要记住的一件事,显式继承胜过隐式继承.

One thing to always remember, explicit inheritance wins over implicit inheritance.

所以在您的情况下

<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.NoActionBar 继承的 AppTheme.NoActionBar .

In order to have a "NoActionBar" theme, indeed you have to inherit from a style that has that property. So you could create another AppTheme.NoActionBar that inherits from Theme.AppCompat.NoActionBar.

要更好地了解主题/样式,请查看此很棒的 Google I/O 2016 有关样式和主题的链接.

To get a better idea of theme/styles, check this awesome Google I/O 2016 link about styles and themes.

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

10-16 21:16