【问题标题】:Understanding the difference between theme and style attributes了解主题和样式属性之间的区别
【发布时间】:2015-08-06 16:58:47
【问题描述】:

我正在处理 Android L 颜色定义来定义 ListView 的行。我有一个主题:

    <item name="colorAccent">@color/color_blue_dark</item>

在我的行中,我定义了另一个:

<style name="DefaultRow">
    <item name="colorAccent">@color/color_sand</item>
    <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>

假设 colorAccent 值将被覆盖。但如果我在我的行中这样做就不会:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          ...
          style="@style/DefaultRow"/>

但如果我这样做,它会起作用:

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              ...
              android:theme="@style/DefaultRow"/>

有什么区别? colorAccent 是在应用主题时定义的,以后不会被覆盖吗?如何解决这个问题以具有向后兼容性?

【问题讨论】:

  • 感谢您的评论,但我仍然不清楚为什么会发生这种情况,据我了解,样式在本地应用于视图,并将主题应用于其后代。但是为什么这会改变视图本身的行为呢?
  • android:theme 属性应用于视图本身及其后代。在内部,它创建一个 ContextThemeWrapper 并针对该上下文扩展视图及其后代。

标签: android android-theme android-styles


【解决方案1】:

TextView 没有 colorAccent 属性。它是一个主题属性。

【讨论】:

  • 实际上,确实如此,我使用 ?android:attr/activatedBackgroundIndicator 作为背景属性并检查stackoverflow.com/questions/15008150/… 这个答案我假设它在后面使用了 colorAccent
  • 但它仍然是一个主题属性,而不是样式属性。这就是为什么它不适用于样式。它是 Android 5 及更高版本独有的功能
猜你喜欢
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2019-10-30
  • 2011-10-26
  • 2011-05-16
  • 1970-01-01
  • 2012-10-03
相关资源
最近更新 更多