事情是这样的:在我的 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 它可以工作,但颜色完全不同。我该如何修复它?
最佳答案
所以这是样式/它们的继承问题。有两种方法可以从样式继承属性。
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.NoActionBar
的 AppTheme.NoActionBar
。
要更好地了解主题/样式,请查看这个很棒的 Google I/O 2016有关样式和主题的链接。
关于Android 风格传承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38083262/