导航抽屉:Gmail与AppCompatv7 v21的对比

5
我想要将我的导航抽屉改成新版Gmail应用程序的样式。我正在使用AppCompatv7 - v21并拥有最新的sdk,但我缺少什么呢?请参考下面的图片。
Gmail导航: enter image description here 导航抽屉会移动到工具栏上方。
我的当前导航: enter image description here 导航抽屉位于工具栏下方。
[编辑]
这是我早期的XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_with_spinner" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>

        <ListView
            android:id="@+id/listview_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/dark_grey"
            android:choiceMode="singleChoice"
            android:divider="@drawable/drawer_list_divider"
            android:dividerHeight="2dp" />
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

根据Pedro的建议,我尝试将工具栏放入DrawerLayout中。

以下是我的新XML:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_with_spinner" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@drawable/drawer_list_divider"
        android:dividerHeight="2dp" />

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

这是我的onCreate()方法中的当前代码:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        spinner = (Spinner) toolbar.findViewById(R.id.spinner);
        mDrawerList = (ListView) findViewById(R.id.listview_drawer);

现在,我甚至看不到工具栏。这是图片。“enter [编辑]
这是我的新布局。这个有效... 再次感谢 Pedro...
<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_with_spinner" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@drawable/drawer_list_divider"
        android:dividerHeight="2dp" />

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

enter image description here

2个回答

5

你需要将工具栏放在抽屉布局里面。

以下是一个XML示例,取自这个 Github 项目:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                        xmlns:app="http://schemas.android.com/apk/res-auto"
                                        android:id="@+id/drawer"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar_actionbar"
            layout="@layout/toolbar_default"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
    <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.poliveira.apps.materialtests.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>

我试图将工具栏放到抽屉布局中,但现在我根本看不到工具栏。你能看看我的编辑过的问题吗? - Vamsi Challa
将您的工具栏和FrameLayout放入一个LinearLayout中。请查看我的XML示例。 - Pedro Oliveira
谢谢,现在它可以工作了...我之前尝试添加LinearLayout,但由于某种原因它崩溃了。看来我当时搞砸了。现在它运行良好。感谢您的帮助!!! - Vamsi Challa
它在主视图上添加视图... 我想将其添加到“导航抽屉”的“ListView”中 - Pratik Butani

0

这是我的新布局。它有效了...再次感谢Pedro...

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_with_spinner" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@drawable/drawer_list_divider"
        android:dividerHeight="2dp" />

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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接