包含布局的导航抽屉布局

时间:2014-07-07 14:35:20

标签: android android-layout navigation-drawer

我认为我的问题其实很简单,但我无法弄清楚如何解决它。 a有一个带有以下代码的工作导航抽屉:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="5dp" >

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

        <ListView
            android:id="@+id/category_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>

    </FrameLayout>

    <!-- The navigation drawer -->
    <ListView 
        android:id="@+id/left_drawer"
        android:entries="@array/features"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:background="#E0E0E0"
        android:dividerHeight="0dp"
        />    
</android.support.v4.widget.DrawerLayout>

但是当我尝试使用包含这样的内容时:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="5dp" >

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

        <ListView
            android:id="@+id/category_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>

    </FrameLayout>

    <!-- The navigation drawer -->
    <include layout="@layout/drawer"/>

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

当我在框架布局中单击列表视图的项目时,没有任何反应。

这是抽屉布局,我试图包括:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="5dp" >

    <!-- The navigation drawer -->
    <ListView 
        android:id="@+id/left_drawer"
        android:entries="@array/features"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:background="#E0E0E0"
        android:dividerHeight="0dp"
        />

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

您所包含的布局会声明第二个android.support.v4.widget.DrawerLayout,它不应该。只需在随附的布局中声明ListView即可。

文件 drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/left_drawer"
    android:entries="@array/features"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@null"
    android:background="#E0E0E0"
    android:dividerHeight="0dp"
    />    

答案 1 :(得分:0)

我使用了我的商品RelativeLayoutNavigationView内的包含。

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/fundo_app"
        tools:visibility="visible">

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

</android.support.design.widget.NavigationView>