如何在ConstraintLayout中使用DrawerLayout

4
我有一个带DrawerLayout的android.support.design.widget.NavigationView在工作中运作正常,但现在我想将它移植到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>

将布局迁移到ConstraintLayout:(抽屉无法滑动,失效了) "@+id/frame" 是我插入Fragment的地方。
<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>

1
从父级中删除约束布局,仅将其包含在抽屉的内容部分中。 - kandroidj
谢谢,你能具体一点吗? - Tord Larsen
3
抽屉(在这种情况下是NavigationView)仍需要成为DrawerLayout的最后一个直接子元素。也就是说,将其移动到闭合的CoordinatorLayout标签之后。 - Mike M.
谢谢,那行了。现在我又遇到了另一个很酷的问题 - Tord Larsen
3个回答

1
你可以尝试这个..

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

0

你可以将按钮和框架布局小部件合并到限制布局中,而不是线性布局。但是,您应该使用线性布局来定义NavHostFragment,而可以将NavigationView单独定义为DrawerLayout的最后一个子项。注意:在此LinearLayout和ConstraintLayout是DrawerLayout的2个子项。LinearLayout保留了NavHostFragment,ConstraintLayout保留了Toolbar、FrameLayout中定义的小部件和退出按钮。Drawer Layout将是第3个子项。


0

抱歉,来晚了,但我不明白如何使用顶部答案,因为Android Studio阻止这样做。

因此,我进一步调查发现以下方法可行:

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.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/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <androidx.constraintlayout.widget.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="wrap_content"
        tools:context=".MainActivity"
        tools:ignore="HardcodedText">

        <!--your ConstraintLayout code here, like this button -->
        <Button
            android:id="@+id/butt"
            android:layout_width="60dp"
            android:layout_height="36dp"
            android:layout_marginTop="5dp"
            android:text="30"
            app:backgroundTint="@color/teal_200"
            app:layout_constraintEnd_toStartOf="@+id/butt"
            app:layout_constraintStart_toEndOf="@+id/butt"
            app:layout_constraintTop_toBottomOf="@+id/editTextIzvornaValuta" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.drawerlayout.widget.DrawerLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接