导航抽屉布局与包含布局

3

我觉得我的问题其实很简单,但是我无法解决它。 我有一个工作中的导航抽屉,代码如下:

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

但是当我尝试像这样使用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" >

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

感谢您的帮助!

我正在考虑像你在这里做的那样,使用<include/>添加我的导航抽屉。你这样做是为了提高性能或节省内存,还是只是为了简化代码? - seekingStillness
1
@seekingStillness 我只是为了简化代码而这样做的。我无法确定它是否以及如何提高性能 :s - Rob
2个回答

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

你能否检查一下这个类似的问题:http://stackoverflow.com/questions/32225679/android-including-drawer-in-all-activities-xml-and-calling-them-when-clicked - We are Borg
我已经尝试使用include,但它不起作用,我必须直接将视图粘贴到DrawerLayout中,这很奇怪,因为Include视图不应该改变任何东西。 - Zapnologica

0

我用RelativeLayout布局放置了我的项目,然后在NavigationView中使用了include

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

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