actionButtonStyle 不适用于支持库 v23.1.0

actionButtonStyle doesn't work with support lib v23.1.0

在最新的支持库版本 (v23.1.0) 中,我注意到其中一个主题属性不再适用于我。我正在使用 actionButtonStyle 主题属性来自定义工具栏中的操作按钮:

<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
    ......
    <item name="actionButtonStyle">@style/Custom.Widget.AppCompat.ActionButton</item>
    ......
</style>

<style name="Custom.Widget.AppCompat.ActionButton" parent="Widget.AppCompat.ActionButton">
    <item name="textAllCaps">false</item>
</style>

它与支持库 v23.0.1 完美配合,但不再与 v23.1.0 配合使用。

所以问题是 - 这是自定义工具栏操作按钮的正确方法吗?如果没有,使用最新支持库版本的正确方法是什么?

试图询问第一方 (https://code.google.com/p/android/issues/detail?id=191544) - 但得到了 "we don't care" 回复:(

让我们开始了解为什么v7支持库v23.1.0会出现这个问题。

AppCompatTextHelper v23.0.1:

// Now check TextAppearance's textAllCaps value
if (ap != -1) {
    TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance);
    if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) {
        setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false));
    }
    appearance.recycle();
}

// Now read the style's value
a = context.obtainStyledAttributes(attrs, TEXT_APPEARANCE_ATTRS, defStyleAttr, 0);
if (a.hasValue(0)) {
    setAllCaps(a.getBoolean(0, false));
}
a.recycle();

AppCompatTextHelper v23.1.0:

// Now check TextAppearance's textAllCaps value
if (ap != -1) {
    TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance);
    if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) {
        setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false));
    }
    appearance.recycle();
}

// Now read the style's value
a = context.obtainStyledAttributes(attrs, TEXT_APPEARANCE_ATTRS, defStyleAttr, 0);
if (a.getBoolean(0, false)) {
    setAllCaps(true);
}
a.recycle();

如您在 v23.1.0 中所见,仅当样式的值为 true 时才会调用 setAllCaps。这就是为什么当值为 false 时没有任何反应。在以前的版本中,每次有值时都会调用 setAllCaps

如何像版本 23.0.1 一样恢复属性 textAllCaps

将这些行添加到 styles.xml:

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="actionButtonStyle">@style/Custom.Widget.AppCompat.ActionButton</item>
    <item name="actionMenuTextAppearance">@style/Custom.TextAppearance.AppCompat.Widget.ActionBar.Menu</item>
</style>

<style name="Custom.TextAppearance.AppCompat.Widget.ActionBar.Menu" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="textAllCaps">false</item>
</style>

<style name="Custom.Widget.AppCompat.ActionButton" parent="Widget.AppCompat.ActionButton">
    <item name="textAllCaps">false</item>
</style>

将主题应用到您的工具栏:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/AppTheme.AppBarOverlay"/>