本文介绍了在DrawerLayout中使用LinearLayout而不是RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我为导航抽屉使用了固定菜单,因此我尝试在DrawerLayout的第二个子菜单中使用LinearLayout而不是RecyclerView/ListView来简化编码.但是结果是全屏线性布局.有什么方法可以在DrawerLayout中使用常规视图(如linearlayout)?

Since I'm using a fixed menu for the navigation drawer I tried to use LinearLayout in DrawerLayout's second child instead of RecyclerView/ListView to make it easier to code. But the result is a full-screen linearlayout. Is there any way to use a regular view (like linearlayout) in DrawerLayout?

<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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bye world"/>
</LinearLayout>

推荐答案

任何View都可以充当DrawerLayout中的抽屉.为了使DrawerLayout识别View为抽屉,必须将layout_gravity属性设置为left/rightstart/end,具体取决于您希望抽屉的垂直边缘附着.

Any View can act as a drawer in a DrawerLayout. For the DrawerLayout to recognize a View as a drawer, it must have a layout_gravity attribute set to left/right or start/end, depending on which vertical edge you want the drawer attached to.

抽屉的layout_height通常为match_parent,因此抽屉跨越DrawerLayout的整个高度,并且抽屉的layout_width通常是精确的尺寸-例如240dp-以保持一致的宽度,与内容无关.

The drawer's layout_height is normally match_parent, so that the drawer spans the full height of the DrawerLayout, and its layout_width is normally an exact measure - e.g., 240dp - to keep a consistent width, independent of its contents.

此外,抽屉View必须在DrawerLayout中最后列出,以保持正确的z顺序,否则它将被内容View覆盖,并且不会收到点击事件.

Additionally, the drawer View must be listed last within the DrawerLayout to maintain the correct z-order, otherwise it will be covered by the content View and won't receive click events.

这篇关于在DrawerLayout中使用LinearLayout而不是RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 05:13