用 support.v7 版本替换已弃用的 android.support.v4.app.ActionBarDrawerToggle 导致抽屉在 Jelly Bean 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26442135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:27:34  来源:igfitidea点击:

Replace deprecated android.support.v4.app.ActionBarDrawerToggle with support.v7 version cause drawer not works on Jelly Bean

androidandroid-support-library

提问by AndreaF

Following the answer in this questioni have replaced ActionBarDrawerToggleof support v4 library that in latest update(rev 21) has been deprecated with the latest ActionBarDrawerToggleof support-v7 library.

按照这个问题的答案,我已经替换ActionBarDrawerToggle了最新更新(修订版 21)中的支持 v4 库,该库已被最新ActionBarDrawerTogglesupport-v7 library.

Now the drawer works on Andrid Lollipop Emulator without deprecation warnings but when I test the app on a Jelly Bean real device no drawer and no toggle drawer button is shown.

现在抽屉可以在 Andrid Lollipop Emulator 上运行而没有弃用警告,但是当我在 Jelly Bean 真实设备上测试应用程序时,没有显示抽屉和切换抽屉按钮。

What the hell appened with this support library update? How could I fix this issue without downgrade to previous version?

这个支持库更新到底发生了什么?如何在不降级到以前版本的情况下解决此问题?

Here my layout

这是我的布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!--  content view -->

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/drawer_text" />
    </RelativeLayout>

    <!-- nav drawer -->

    <ListView
        android:id="@+id/drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#F3F3F4"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

回答by mindex

  1. To get ActionBarDrawerTogglev7 to work properly you need to extends your Activity class from android.support.v7.app.ActionBarActivity
  2. ActionBarActivityv7 must be used with Theme.AppCompattheme from the appcompat-v7:21support library.
  3. Unless you want to switch from ActionBarto ToolBar, don'tadd <item name="windowActionBar">false</item>when extending Theme.AppCompat. Doing so will make your ActionBarActivityhave no default ActionBardecor, and getSupportActionBarwill return null. You'll need to provide your own ToolBarand call setSupportActionBarfirst to make getSupportActionBarwork.
  1. 要使ActionBarDrawerTogglev7 正常工作,您需要从android.support.v7.app.ActionBarActivity
  2. ActionBarActivityv7 必须与支持库中的Theme.AppCompat主题一起使用appcompat-v7:21
  3. 除非你想切换ActionBarToolBar<item name="windowActionBar">false</item>扩展时Theme.AppCompat。这样做将使您ActionBarActivity没有默认ActionBar装饰,getSupportActionBar并将返回 null。您需要提供自己的,ToolBarsetSupportActionBar首先致电以进行getSupportActionBar工作。