DrawerLayout中视图的大小和单击行为错误 [英] Wrong size and click behavior for Views in DrawerLayout
问题描述
我有一个Activity
的布局,正在尝试向其添加导航抽屉.
I have a layout for an Activity
that I'm trying to add a navigation drawer to.
问题是,要正常工作,我需要使用:
The problem is, to work properly, I need to use:
<android.support.v4.widget.DrawerLayout
代替:
<RelativeLayout
但是它把事情搞砸了.我的ProgressBar
变得更大了,我的RecyclerView
无法工作,当我单击某些内容时,应用程序将我注销了,等等.
but it messes things up. My ProgressBar
becomes much bigger, my RecyclerView
doesn't work, the app logs me out when I click something, etc.
我的布局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>
如何创建我的抽屉菜单而不弄乱其他所有内容?
How can I create my drawer menu without messing everything else up?
推荐答案
不是抽屉的DrawerLayout
的任何直接子级View
都被视为内容View
,并将其放置在match_parent
中两个方向,无论您在其上设置了width和height属性.在您的情况下-实际上,在大多数情况下-您只需要一个内容View
,因此其余非抽屉View
都应全部放在一个ViewGroup
中.
Any direct child View
of a DrawerLayout
that's not a drawer is considered a content View
, and will be laid out to match_parent
in both directions, regardless of the width and height attributes you've set on it. In your case - indeed, in most cases - you only need one content View
, so the rest of the non-drawer View
s should all be inside a single ViewGroup
.
我们会将您的ProgressBar
和RecyclerView
都放置在用作内容View
的RelativeLayout
内,它们将保留您设置的布局属性.例如:
We'll place your ProgressBar
and RecyclerView
both inside a RelativeLayout
that acts as the content View
, where they'll keep the layout attributes you've set. For example:
<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顺序;即,将抽屉放在内容的顶部.
Note that the content View
should always be listed before any drawers, to maintain proper z-ordering; i.e., to keep the drawers on top of the content.
这篇关于DrawerLayout中视图的大小和单击行为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!