用 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
Replace deprecated android.support.v4.app.ActionBarDrawerToggle with support.v7 version cause drawer not works on Jelly Bean
提问by AndreaF
Following the answer in this questioni have replaced ActionBarDrawerToggle
of support v4 library that in latest update(rev 21) has been deprecated with the latest ActionBarDrawerToggle
of support-v7 library
.
按照这个问题的答案,我已经替换ActionBarDrawerToggle
了最新更新(修订版 21)中的支持 v4 库,该库已被最新ActionBarDrawerToggle
的support-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
- To get
ActionBarDrawerToggle
v7 to work properly you need to extends your Activity class fromandroid.support.v7.app.ActionBarActivity
ActionBarActivity
v7 must be used withTheme.AppCompat
theme from theappcompat-v7:21
support library.- Unless you want to switch from
ActionBar
toToolBar
, don'tadd<item name="windowActionBar">false</item>
when extendingTheme.AppCompat
. Doing so will make yourActionBarActivity
have no defaultActionBar
decor, andgetSupportActionBar
will return null. You'll need to provide your ownToolBar
and callsetSupportActionBar
first to makegetSupportActionBar
work.
- 要使
ActionBarDrawerToggle
v7 正常工作,您需要从android.support.v7.app.ActionBarActivity
ActionBarActivity
v7 必须与支持库中的Theme.AppCompat
主题一起使用appcompat-v7:21
。- 除非你想切换
ActionBar
到ToolBar
,不加<item name="windowActionBar">false</item>
扩展时Theme.AppCompat
。这样做将使您ActionBarActivity
没有默认ActionBar
装饰,getSupportActionBar
并将返回 null。您需要提供自己的,ToolBar
并setSupportActionBar
首先致电以进行getSupportActionBar
工作。