【问题标题】:Using LinearLayout instead of RecyclerView in DrawerLayout在 DrawerLayout 中使用 LinearLayout 而不是 RecyclerView
【发布时间】:2016-02-04 13:12:58
【问题描述】:

由于我为抽屉式导航使用了固定菜单,因此我尝试在 DrawerLayout 的第二个子项中使用 LinearLayout 而不是 RecyclerView/ListView 以使其更易于编码。但结果是全屏线性布局。有没有办法在 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>

【问题讨论】:

  • "有没有办法在 DrawerLayout 中使用常规视图(如线性布局)?" - 是的。您必须正确设置您的 DrawerLayout 及其子 Views。
  • 如何设置 DrawerLayout 以使用 LinearLayout 而不是 RecyclerView?
  • RecyclerView 相同,替换为LinearLayout
  • 抽屉布局的 XML 是什么样的?里面不是有风景吗?
  • 您唯一应该记住的是:第一个孩子是您的主要内容,第二个孩子是您的抽屉内容(据我所知,可以是任何视图组)。

标签: android android-layout android-linearlayout android-recyclerview navigation-drawer


【解决方案1】:

任何View 都可以作为DrawerLayout 中的抽屉。要让DrawerLayoutView 识别为抽屉,它必须将layout_gravity 属性设置为left/rightstart/end,具体取决于您想要抽屉的哪个垂直边缘附在。

抽屉的layout_height 通常是match_parent,因此抽屉跨越DrawerLayout 的整个高度,而其layout_width 通常是精确的度量——例如240dp——以保持一致的宽度,与其内容无关。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2021-04-15
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2018-12-12
    • 2016-02-29
    相关资源
    最近更新 更多