微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – DrawerLayout中视图的大小和单击行为错误

我有一个活动的布局,我正在尝试添加导航抽屉.

问题是,要正常工作,我需要使用:

<android.support.v4.widget.DrawerLayout

代替:

<RelativeLayout 

但它搞砸了.我的ProgressBar变得更大,我的RecyclerView不起作用,当我点击某些内容时,应用程序会将我注销等.

我的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.st.mf.UserAreaActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="@dimen/activity_vertical_margin"
    android:background="#fff">

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dp" />

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@layout/navigation_menu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

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

如何在不弄乱其他所有内容的情况下创建抽屉菜单

解决方法:

任何不是抽屉的DrawerLayout的直接子视图被视为内容视图,并且将在两个方向上布局为match_parent,而不管您在其上设置的宽度和高度属性.在您的情况下 – 实际上,在大多数情况下 – 您只需要一个内容视图,因此其余的非抽屉视图应该都在一个ViewGroup中.

我们将ProgressBar和RecyclerView放在作为内容视图的RelativeLayout中,它们将保留您设置的布局属性.例如:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.st.mf.UserAreaActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin"
    android:background="#fff">

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

        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="20dp" />

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@layout/navigation_menu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

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

请注意,内容View应始终在任何抽屉之前列出,以保持正确的z排序;即,将抽屉保持在内容的顶部.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐