【问题标题】:Navigation Drawer: Gmail vs AppCompatv7 v21导航抽屉:Gmail 与 AppCompatv7 v21
【发布时间】:2014-11-12 10:56:51
【问题描述】:

我正在尝试将我的导航抽屉更改为类似于新的 Gmail 应用程序的导航抽屉。我正在使用 AppCompatv7 - v21,并拥有更新的 sdk。我错过了什么?请参考以下图片。

Gmail 导航:

导航抽屉,在工具栏上移动。

我目前的导航:

导航抽屉位于工具栏下方。

[编辑]

这是我早期的 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 的建议,我尝试将工具栏移动到抽屉布局中。

这是我的新 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);

现在,我什至看不到工具栏。这是图片。

[编辑]

这是我的新布局。这行得通.. 再次感谢佩德罗...

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

【问题讨论】:

    标签: android navigation-drawer android-toolbar


    【解决方案1】:

    您必须将工具栏放在抽屉布局中。

    这里是一个使用from this Github Project:的XML示例

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

    【讨论】:

    • 我试图将工具栏放在抽屉布局中,但现在我根本看不到工具栏。你能看看我编辑的问题吗?
    • 将您的工具栏和框架布局包装在一个线性布局中。检查我的示例 XML
    • 谢谢,现在可以了...我之前尝试添加线性布局,但由于某种原因它崩溃了。好像我当时搞砸了。现在它工作正常。谢谢你的帮助!!!
    • 它在主视图上添加视图..我想添加ListView of NavigationDrawer
    【解决方案2】:

    这是我的新布局。这行得通.. 再次感谢佩德罗...

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

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 2016-04-18
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多