appcompat v22.1 小部件着色不显示 pre 5.0

appcompat v22.1 widget tinting doesn't show pre 5.0

我正在使用 AppCompat lib v22.1 为多个 android 版本提供一些 material 设计。如果我使用新的小部件着色功能,即使使用 AppCompat,它也不会出现在 Kitkat 中。

布局中:

<Button
            android:id="@+id/mybtn"
            android:text="Test"
            android:theme="@style/MyTheme.BlueButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

在主题中 xml:

<style name="MyTheme.BlueButton">
    <item name="colorButtonNormal">@color/blue</item>
</style>

这是我做错了什么还是 AppCompat 中的错误?

通过使用以下设置,我能够在 Kitkat 和 Lollipop 上获得预期的结果:

build.gradle(部分)

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.1'
}

values/styles.xml

<resources>

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

    <style name="ThemeOverlay.AppTheme.Blue" parent="ThemeOverlay.AppCompat.Dark">
        <!-- ThemeOverlay themes define only some attributes, those which make
            backgrounds dark and highlights and texts light for ThemeOverlay.AppCompat.Dark
            or the other way around for .Light. The rest is taken from the activity theme.
            You only customize what you need - here the button color. -->
        <item name="colorButtonNormal">@color/blue_500</item>
    </style>

    <style name="ThemeOverlay.AppTheme.Yellow" parent="ThemeOverlay.AppCompat.Light">
        <!-- This worked correctly even with parent="Theme.AppTheme". -->
        <item name="colorButtonNormal">@color/yellow_500</item>
    </style>

</resources>

layout/activity_main.xml(部分)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:theme="@style/ThemeOverlay.AppTheme.Blue"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:theme="@style/ThemeOverlay.AppTheme.Yellow"/>

编辑:(哦,是的,我忘了)MainActivity.java

public class MainActivity extends AppCompatActivity { ... }

结果

我发现了为什么原来的代码在我认为应该的时候不起作用。我使用的是来自支持 v4 lib 的 FragmentActivity,它似乎没有内置主题的新 v7 AppCompat 功能。将我的 activity 更改为 AppCompatActivity 可以使主题正常工作,而我所拥有的没有任何变化。

此处有更多技术说明: