Android DrawerLayout - No drawer view found with gravity

前端 未结 9 466
执笔经年
执笔经年 2020-11-27 06:23

When I click on my drawer toggle I get the following exception:

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

相关标签:
9条回答
  • 2020-11-27 07:12

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

    SOLUTION Assign a layout_gravity = "start" or "left" attribute to one of your DrawerLayout child view if your drawer layout already have a child view. OR Simply create a child view inside your DrawerLayout View and give it a layout_gravity = "start" or "left" attribute. for example

     <?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.CoordinatorLayout 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:id="@+id/mDrawer_Layout"
            tools:context="com.rad5.matdesign.MainActivity">
    
            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <android.support.v4.widget.DrawerLayout
                    android:id="@+id/drawer_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    tools:openDrawer="start">
    
        <!-- Notice that here an image view as a child of the Drawer layout was -->
        <!-- given a layout_gravity="start" -->
    
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start"/>
    
                </android.support.v4.widget.DrawerLayout>
    
                <android.support.design.widget.AppBarLayout
                    android:id="@+id/appBar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="?attr/colorPrimary"/>
    
                    <android.support.design.widget.TabLayout
                        android:id="@+id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </android.support.design.widget.AppBarLayout>
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
            </android.support.design.widget.CoordinatorLayout>
    
            <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:headerLayout="@layout/nav_header"
                app:menu="@menu/menu_navigation_items" />
    
        </android.support.design.widget.CoordinatorLayout>
    
    0 讨论(0)
  • 2020-11-27 07:15

    The "Android Drawer" needs to be a direct child node of the "DrawerLayout".

    In your example above (re: original question example), you only have a single direct child node under "DrawerLayout" ("LinearLayout"), and no drawer view after it.

    Move your drawer view out of your LinearLayout and place it after it.

    0 讨论(0)
  • 2020-11-27 07:19

    add

    android:layout_gravity="start"

    to NavigationView

    like this

    <com.google.android.material.navigation.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/drawer_menu"
        android:layout_gravity="start"
        android:foregroundGravity="right"
        android:id="@+id/nv">
    
        <include layout="@layout/drawer_menu"/>
    </com.google.android.material.navigation.NavigationView>
    
    0 讨论(0)
提交回复
热议问题