【问题标题】:Howto use the DrawerLayout with ConstraintLayout如何将 DrawerLayout 与 ConstraintLayout 一起使用
【发布时间】:2017-06-25 04:16:17
【问题描述】:

我的 android.support.design.widget.NavigationView 和 DrawerLayout 工作正常,但现在我想将它移植到 ConstraintLayout。这不太顺利,想请教一下?

原始工作 XML:

    <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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.port.android.ui.ActivityMain">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />

        <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_view_header"
        app:menu="@menu/menu_navigation">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:padding="20dp">

            <Button
                android:id="@+id/btn_exit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="exitAddress"
                android:text="Exit Address" />

        </LinearLayout>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

PORTING TO ConstraintLayout:(不工作的抽屉是陈旧的,不能刷入/刷出)

"@+id/frame" 是我插入 Frags 的地方

<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.port.android.ui.ActivityMain">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar"
            android:layout_width="0dp"
            android:layout_height="56dp"
            tools:layout_editor_absoluteY="100dp"
            tools:layout_editor_absoluteX="8dp" />

        <android.support.constraint.ConstraintLayout
            android:id="@+id/frame"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.22000003"
            android:layout_marginStart="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginLeft="16dp">

        </android.support.constraint.ConstraintLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_view_header"
            app:menu="@menu/menu_navigation"
            android:fitsSystemWindows="true"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp">

            <Button
                android:id="@+id/btn_exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="exitAddress"
                android:text="Exit Address" />

        </android.support.design.widget.NavigationView>
    </android.support.constraint.ConstraintLayout>

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

【问题讨论】:

  • 从父级移除约束布局,只包含在抽屉的内容部分
  • 谢谢,你能说得更具体点吗..
  • 抽屉(在这种情况下是您的NavigationView)仍然需要是DrawerLayout 的最后一个直接子级。即,将其移至结束 CoordinatorLayout 标记之后。
  • 谢谢这工作。现在我有another cool problem

标签: android drawerlayout android-constraintlayout


【解决方案1】:

您可以在约束布局而不是线性布局中组合按钮和框架布局小部件。但是,您应该使用线性布局来定义 NavHostFragment,并且可以单独将 NavigationView 定义为 DrawerLayout 的最后一个子项。注意:这里 LinearLayout 和 ConstraintLayout 是抽屉布局的 2 个孩子。线性布局包含您的 NavHostFragment,ConstraintLayout 包含您的工具栏、您在 FrameLayout 中定义的小部件和退出按钮。抽屉布局将是第三个孩子。

【讨论】:

    【解决方案2】:

    你可以试试这个..

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.constraint.ConstraintLayout     
            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:layout_width="match_parent"
            android:layout_height="match_parent">
        
        <android.support.v4.widget.DrawerLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">    
        </android.support.v4.widget.DrawerLayout>
        
        </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多