DrawerLayout

时间:2017-02-22 08:23:40

标签: android android-layout drawerlayout

我有Activity的布局,我试图添加导航抽屉。

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

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

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

1 个答案:

答案 0 :(得分:1)

View中不是抽屉的任何直接子DrawerLayout被视为内容View,并且将在match_parent向两个方向展示,无论您在其上设置的宽度和高度属性。在您的情况下 - 实际上,在大多数情况下 - 您只需要一个内容View,因此非抽屉View的其余部分应该都在一个ViewGroup内。

我们会将ProgressBarRecyclerView放在充当内容RelativeLayout的{​​{1}}内,他们会保留您设置的布局属性。例如:

View

请注意,内容<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> 应始终在任何抽屉之前列出,以维持正确的z排序;即,将抽屉放在内容之上。