ActionBarDrawerToggle 在与非 appcompat Theme.Material 一起使用时不理解 appcompat 属性



我在minSdk=21上,使用android.support.v7.app.ActionBarDrawerToggle。我想禁用旋转动画,我这样做:

<style name="MyTheme" parent="android:Theme.Material.NoActionBar">
    <item name="drawerArrowStyle">@style/MyDrawerArrowToggleStyle</item>
</style>
<style name="MyDrawerArrowToggleStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">false</item>
</style>

这导致以下异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.TestActivity}: java.lang.RuntimeException: Failed to resolve attribute at index 0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 0
            at android.content.res.TypedArray.getColor(TypedArray.java:401)
            at android.support.v7.app.DrawerArrowDrawable.<init>(DrawerArrowDrawable.java:69)
            at android.support.v7.app.ActionBarDrawerToggle$DrawerArrowDrawableToggle.<init>(ActionBarDrawerToggle.java:469)
            at android.support.v7.app.ActionBarDrawerToggle.<init>(ActionBarDrawerToggle.java:222)
            at android.support.v7.app.ActionBarDrawerToggle.<init>(ActionBarDrawerToggle.java:150)

当我把这个添加到样式中时:

<item name="color">?attr/colorControlNormal</item>

(它完全是从父样式Widget.AppCompat.DrawerArrowToggle复制过来的),也会出现同样的问题。

当我使用时,问题就消失了

<item name="color">?android:attr/colorControlNormal</item>

即带有"android:"前缀。

当我根本不定义自定义样式时,一切都很好,但正如我所提到的,我不喜欢旋转的图标。

也许值得一提的是,这个问题只发生在"color"属性中,其他属性不需要显式重写。

问题出在哪里?是不是因为我将标准的android属性与appcompat属性混合在一起,而drawer toggle类在这种情况下不知道如何读取没有"android:"前缀的属性?

整个appcompat的东西非常令人困惑,尤其是样式。

我遇到了类似的问题,通过下面的代码片段解决了这个问题。希望它能帮助你!

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Here we setting appcompat's actionBarStyle -->
    <item name="drawerArrowStyle">@style/MaterialDrawer.DrawerArrowStyle</item>
</style>
<style name="MaterialDrawer.DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <!-- The drawing color for the bars -->
    <item name="color">@android:color/white</item>
    <!-- Whether bars should rotate or not during transition -->
    <item name="spinBars">false</item>
</style>

注意:也许应该使用Theme.AppCompat而不是Theme.AppCompat.Light.NoActionBar作为父样式。

最新更新