代码之家  ›  专栏  ›  技术社区  ›  El_Loco

导航视图和约束布局

  •  0
  • El_Loco  · 技术社区  · 5 年前

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

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

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

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

    <?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 约束布局 应该是非常方便和容易调整到不同的屏幕大小。

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

    1 回复  |  直到 5 年前
        1
  •  1
  •   che10    5 年前

    据我所知,目前的问题是 <com.google.android.material.navigation.NavigationView 当您需要ConstraintLayout时,它也驻留在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 如你所需。

    推荐文章