Action bar drawer toggle seizes to work with API 2

2019-08-14 05:01发布

I recently included the appcompat v7 library to my project. In trying to get rid of bunch of errors, I updated most of my SDK files and changed my target SDK version to 23 from 21. The problem is, my old code for designing the Navigation Drawer Toggle (the hamburger sign/arrow sign) now doesn't work and throws and shows errors on the files through eclipse.

Here's the code:

 <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">false</item>
    <item name="barSize">16sp</item>
    <item name="gapBetweenBars">4sp</item>
    <item name="color">@android:color/white</item>
</style>

Here is its implementation in my custom theme:

<item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 

Eclipse error reads: No resource found that matches the given name: attr "barSize".

When I comment that out, the app runs but crashes with the error log:

09-20 01:09:39.852: E/AndroidRuntime(1662): FATAL EXCEPTION: main
09-20 01:09:39.852: E/AndroidRuntime(1662): Process: com.site.app, PID: 1662
09-20 01:09:39.852: E/AndroidRuntime(1662): java.lang.NoSuchMethodError: No static method getLayoutDirection(Landroid/graphics/drawable/Drawable;)I in class Landroid/support/v4/graphics/drawable/DrawableCompat; or its super classes (declaration of 'android.support.v4.graphics.drawable.DrawableCompat' appears in /data/app/com.site.app-2/base.apk)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.support.v7.graphics.drawable.DrawerArrowDrawable.draw(DrawerArrowDrawable.java:337)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.widget.ImageView.onDraw(ImageView.java:1209)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.draw(View.java:15210)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.updateDisplayListIfDirty(View.java:14144)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.getDisplayList(View.java:14167)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.draw(View.java:14934)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3204)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.updateDisplayListIfDirty(View.java:14139)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.getDisplayList(View.java:14167)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.draw(View.java:14934)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3204)
09-20 01:09:39.852: E/AndroidRuntime(1662):     at android.view.View.draw(View.java:15213)

...

This same code worked when I was compiling with API 21. Any ideas on how to style the Drawer Toggle Arrow with API 23?

1条回答
Anthone
2楼-- · 2019-08-14 05:46

You can use android design support libraries on API 23 and modify the Navigation bar. It is easy to use and takes less time to make that as it is just an XML tag. And this library is backward compatible.

Add this to your gradle:

compile 'com.android.support:design:22.2.1'

It is worth modifying the code. As Android design support will reduce a lot of clumsiness needed to design the navigation bar before. A small peice of code will give you a navigation bar.

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<!-- your content layout -->

<android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
查看更多
登录 后发表回答