用support.v7版本替换已弃用的android.support.v4.app.ActionBarDrawerToggle导致抽屉不适用于Jelly Bean

时间:2014-10-18 16:41:43

标签: android android-support-library

this question中的答案后,我已替换ActionBarDrawerToggle支持v4库,其中最新更新(rev 21)已被弃用最新ActionBarDrawerToggle support-v7 library

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

这个支持库更新到底是什么?如果不降级到以前的版本,我怎么能解决这个问题?

这是我的布局

<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>

1 个答案:

答案 0 :(得分:6)

  1. 要让ActionBarDrawerToggle v7正常运行,您需要从android.support.v7.app.ActionBarActivity
  2. 扩展您的Activity类
  3. ActionBarActivity v7必须与Theme.AppCompat支持库中的appcompat-v7:21主题一起使用。
  4. 除非您希望从ActionBar切换到ToolBar,否则 在扩展<item name="windowActionBar">false</item>时添加Theme.AppCompat。这样做会使ActionBarActivity没有默认ActionBar装饰,getSupportActionBar将返回null。您需要首先提供自己的ToolBar并致电setSupportActionBar以使getSupportActionBar正常工作。