Android导航抽屉和工具栏(api 21)

时间:2014-11-10 21:01:40

标签: android navigation-drawer drawerlayout

我的导航抽屉有问题(我使用api 21)。

我的布局:

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

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

        <LinearLayout
            android:id="@+id/drawer_linear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!-- Toolbar -->
            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#0000FF" />


            <!-- Listview to display slider menu -->
            <ListView
                android:id="@+id/list_slidermenu"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"
                android:divider="@color/list_divider"
                android:dividerHeight="1dp"       
                android:listSelector="@drawable/list_selector"
                android:background="@color/list_background"/>

        </LinearLayout>

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

错误:查看android.widget.linearLayout app:id / drawer_linear不是滑动抽屉。 这个布局有什么问题?

感谢您的帮助,

2 个答案:

答案 0 :(得分:0)

这种布局适合我:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<include layout="@layout/main_toolbar"/>

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

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"/>


<LinearLayout
    android:id="@+id/linear_layout_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:layout_gravity="start"
    android:orientation="vertical">

<fragment android:id="@+id/navigation_drawer"
    android:tag="drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:name="me.myapp.drawer.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

</LinearLayout>

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

</LinearLayout>

答案 1 :(得分:0)

无需在线性布局下同时包含列表视图和工具栏。 在FrameLayout中添加片段。

这有效:

Drawer.xml

<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:orientation="vertical"
    tools:context="com.example.toolbar.Drawer" >

<android.support.v7.widget.Toolbar 
    xmlns:app1="http://schemas.android.com/apk/res/com.example.toolbar"
    android:id="@+id/my_awesome_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="75dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app1:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app1:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

</android.support.v7.widget.Toolbar>

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

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

        <!-- Nav drawer -->

        <ListView
            android:id="@+id/drawerList"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="@android:color/white"
            android:divider="@android:color/white"
            android:dividerHeight="8dp"
            android:drawSelectorOnTop="true"
            android:headerDividersEnabled="true" />
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>