首页 > 解决方案 > ContextThemeWrapper 不适用于样式中定义的属性

问题描述

ContextThemeWrapper 非常适合以编程方式为膨胀视图设置新主题。

这适用于以下情况:

<TextView
  android:textColor="?attr/textColorPrimary"
  />

但令人惊讶的是不适用于:

<TextView
  style="@style/Label"
  />
<style name="Label">
  <item name="android:textColor">?attr/textColorPrimary</item>
</style>

有没有办法让样式从用于膨胀视图的 ContextThemeWrapper 中读取正确的属性值?

标签: androidandroid-layoutandroid-themeandroid-styles

解决方案


在这种情况下,您必须应用主题覆盖而不是样式:

        <TextView
            android:theme="@style/Label"
            ../>

推荐阅读