【问题标题】:Navigation View and Constraint Layout导航视图和约束布局
【发布时间】:2021-06-05 16:05:39
【问题描述】:

我正在尝试制作新应用,同时学习 Kotlin 并使用 Android 的新功能。

在我的main_activity 中,我尝试创建一个应该始终存在的导航菜单和工具栏(我认为这是我尝试使用的单活动模式)。

当我尝试关闭菜单时,应用程序崩溃,因为它找不到重力左/启动的抽屉。

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

这是xml:

<?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"
    android:id="@+id/navigation_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/camera_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/main_app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_blue_light"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/main_navigation"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/main_app_bar_layout"
            app:menu="@menu/navigation_menu" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>

如果我改为删除 ConstraintLayout 它会起作用:

<?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"
    android:id="@+id/navigation_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/main_app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/holo_blue_light"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/main_navigation"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/main_app_bar_layout"
            app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

请注意,我必须将marginTop 添加到导航视图并将高度添加到操作栏。据我了解,ConstraintLayout 应该很方便,很容易适应不同的屏幕尺寸。

在这种特殊情况下,获得正确的边距并不难,但在许多其他情况下可能很难。

有没有办法在导航视图中使用约束布局?这里有什么问题?

【问题讨论】:

  • 您希望将以上视图中的哪个视图用作抽屉布局?请你澄清一下。我认为约束布局在这里没有错。我一起使用抽屉布局和约束布局,所以没有这样的问题。
  • 请试试这个,你会从这个link得到答案

标签: android android-constraintlayout android-navigationview


【解决方案1】:

据我了解,目前的问题是您的 &lt;com.google.android.material.navigation.NavigationView 也位于 ConstraintLayout 内部,而您在外部需要它。

这是我认为您需要的更改后的 XML。

<?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"
    android:id="@+id/navigation_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/camera_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/main_app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_blue_light"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/main_navigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:actionBarSize"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_app_bar_layout"
        app:menu="@menu/bottom_nav_bar_provider" />

</androidx.drawerlayout.widget.DrawerLayout>

请注意,我已将您的NavigationView 移出并根据您的需要为其分配了?android:actionBarSize 的上边距。

只是视图的顺序搞砸了。 ConstraintLayout 非常可靠。

【讨论】:

  • 谢谢,是什么迫使你在外面有导航视图?
  • 对于Navigation View 要成为Drawer Layout 的一部分,它必须是它的直接子级。当你把它放在ConstraintLayout 中时,层次结构变为DrawerLayout -&gt; ConstraintLayout -&gt; NavigationView,因此不是直接子级。因此,DrawerLayout 的任何属性都不适用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2023-04-07
  • 2017-10-09
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多