Android 风格传承

标签 android

事情是这样的:在我的 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.NoActionBarAppTheme.NoActionBar

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

关于Android 风格传承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38083262/

相关文章:

java - Android - 无法初始化 Google 地方信息

java - Android 应用程序的 android.os.NetworkOnMainThreadException

java - Android Studio : Error:/android-studio/gradle/gradle-2. 4/lib/plugins/gradle-diagnostics-2.4.jar(没有这样的文件或目录)

android - 如何通过Glide SDK 4.3.1在touchimageview中加载位图?

c# - Activity 需要 FLAG_ACTIVITY_NEW_TASK 标志

android - 用于自定义 android editText 的库,每个字母都有单独的框(例如 otp/pin View ),间距可变以容纳多个单词

Java android 应用程序点击按钮时出现致命异常

java - 从 textView 中提取一部分文本并使该文本可点击?

android - INTEGER 还是一个字符的 TEXT 哪个更有效?

java - 如何修复 "Failed to notify build listener"错误?