navigation drawer layout with include layout

血红的双手。 提交于 2019-12-09 19:06:50

问题


i think my problem is actually quite simple, but i can't figure out how to solve it. a have a working navigation drawer with this code:

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

but when i try do use include like this:

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

nothing happens when i click on the items of my list view within my frame layout.

this is the drawer layout i'm trying to include:

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

thanks for helping!


回答1:


Your included layout declares a second android.support.v4.widget.DrawerLayout, it shouldn't. Just declare a ListView in your included layout.

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



回答2:


I did a RelativeLayout with my items, and the include inside NavigationView.

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


来源:https://stackoverflow.com/questions/24613154/navigation-drawer-layout-with-include-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!