DrawerLayout正在与其他项重叠

时间:2015-08-12 16:48:30

标签: android xml

我正在尝试建立一个菜单。

要构建此菜单,我正在使用DrawerLayout。

但是我有一些问题显示DrawerLayout,因为我的Listview重叠了所有内容。

我需要更改我的XML代码才能防止这种情况发生?

我的菜单:

enter image description here

ListView重叠菜单

enter image description here

我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <include
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/toolbar" />

    <!-- Menu -->

    <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:layout_below="@+id/bar" >

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

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#029688"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </android.support.v4.widget.DrawerLayout>

    <!-- Listview is here -->

    <ListView
        android1:id="@+id/lvevents"
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="9dp"
        android1:layout_alignParentLeft="true"
        android1:layout_alignParentRight="true"
        android1:layout_below="@+id/bar"
        android:dividerHeight="6dp"
        android:scrollbars="none" >
    </ListView>

    <ImageView
        android1:id="@+id/imgvloading"
        android1:layout_width="64dp"
        android1:layout_height="64dp"
        android:layout_centerInParent="true"
        android:gravity="center" />

</RelativeLayout>

感谢。

3 个答案:

答案 0 :(得分:1)

您的DrawerLayout应该是根布局,例如:

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

    <!-- Place your main content here -->
    <FrameLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/frame_parent"/>

    <!-- And the layout for your nav menu here -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/navigation_menu_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:scrollbars="vertical"
        android:background="#ff1cac"
        />

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

答案 1 :(得分:1)

通常,DrawerLayout是活动的根布局。 DrawerLayout的第一个子视图是您的内容,第二个是您的实际抽屉。尝试重新安排跟随并根据需要进行调整:

<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"
    android:background="@color/background"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android_layout_height="match_parent">

        <include
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/toolbar" />

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

        <ListView
            android:id="@+id/lvevents"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="9dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/bar"
            android:dividerHeight="6dp"
            android:scrollbars="none" />

        <ImageView
            android:id="@+id/imgvloading"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_centerInParent="true"
            android:gravity="center" />

    </RelativeLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#029688"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

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

此外,您应删除xmlns:android1的不必要声明,并将您使用android的所有实例替换为{我已提供的示例中已完成此操作)

答案 2 :(得分:0)

您的列表视图&#39;必须放在您的内容框架内#39;和&#39; DrawerLayout&#39;应该用作根视图:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/bar">

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

        <include
            android:id="@+id/bar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <ListView
            android1:id="@+id/lvevents"
            android1:layout_alignParentLeft="true"
            android1:layout_alignParentRight="true"
            android1:layout_below="@+id/bar"
            android1:layout_height="wrap_content"
            android1:layout_width="match_parent"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="9dp"
            android:dividerHeight="6dp"
            android:scrollbars="none">
        </ListView>
    </LinearLayout>


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#029688"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>