【问题标题】:Android DrawerLayout - No drawer view found with gravityAndroid DrawerLayout - 没有发现重力的抽屉视图
【发布时间】:2014-12-08 01:39:18
【问题描述】:

当我点击抽屉开关时,出现以下异常:

java.lang.IllegalArgumentException:没有找到重力的抽屉视图 左

这是我的activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <fragment
        android:id="@+id/navigation"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.xyz.ui.navigation.NavigationFragment"
        tools:layout="@layout/fragment_navigation" />

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

我的fragment_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start">

</ListView>

还有我的列表项:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/navigation_item_text"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

【问题讨论】:

  • 在某些设备上 START==LEFT 在另一个 ==RIGHT 上,它用于 RTL 从右到左的书写支持。在所有设备上使用STARTLEFT 以固定到选定的一侧
  • @MikeM。不,在我的 Java 代码中我不使用它;)
  • @Tobias :有什么特别的原因你在你的activity_drawer.xml 中使用Fragment,然后只是使用一个只有ListView 的布局吗?如果您从 activity_drawer.xml 中删除 &lt;fragment ... /&gt; 块并将其替换为 ListView 会发生什么?
  • 您的抽屉需要在 LinearLayout 之外。
  • 如果你仔细检查,你会发现确实有两个顶级元素:LinearLayout 和 fragment。

标签: android navigation-drawer


【解决方案1】:

来自文档

要使用 DrawerLayout,请将您的主要内容视图定位为第一个子视图,其宽度和高度为 match_parent。在主要内容视图之后添加抽屉作为子视图,并适当地设置 layout_gravity。抽屉通常使用 match_parent 作为高度和固定宽度。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    </LinearLayout>

    <fragment
        android:id="@+id/navigation"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:name="com.xyz.ui.navigation.NavigationFragment"
        tools:layout="@layout/fragment_navigation" />

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

【讨论】:

  • 简单的解决方案并为我解决了这个错误。谢谢!
【解决方案2】:


好的。让我在这里把事情简单明了。
DrawerLayout 是什么?https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

DrawerLayout 充当窗口内容的顶级容器,允许 从边缘拉出交互式“抽屉”视图 窗户。使用android:layout_gravity 控制抽屉定位和布局 子视图上的属性对应于视图的哪一侧 希望抽屉从:左或右出现。 (或开始/结束于 支持布局方向的平台版本。)使用 DrawerLayout,将您的主要内容视图定位为第一个具有宽度的子视图 和match_parent 的高度。在主要内容视图之后添加抽屉作为子视图并设置 layout_gravity 适当。抽屉通常使用match_parent 表示高度和固定宽度。


什么是DrawerLayout?,简单来说:
  • Android 支持的布局允许您在主屏幕上放置一个覆盖屏幕,称为抽屉。
  • DrawerLayout 需要子视图才能工作,类似于 LinearLayout 和 RelativeLayout。
  • 子视图必须具有任一属性集
    android:layout_gravity="start"android:layout_gravity="left"
  • DrawerLayout 使用这个 childView 来了解 从哪里开始显示 Drawer 即从左到右或从右到 左边。有些国家的文本是从右向左阅读的。


技术人员资料:
  • EVENT#1:ActionBarDrawerToggle.java 被设置为 DrawerLayout 的抽屉监听器
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 工具栏,R.string.navigation_drawer_open, R.string.navigation_drawer_close) mDrawerLayout.setDrawerListener(mDrawerToggle);
  • EVENT#2:用户单击调用toggle() 函数的 Toolbar.navigationIcon
    ActionBarDrawerToggle.java (android-sdk\sources\android-22\android\support\v7\app\ActionBarDrawerToggle.java) 私人无效切换(){ if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) { mDrawerLayout.closeDrawer(GravityCompat.START); } 别的 { mDrawerLayout.openDrawer(GravityCompat.START); } }
    Gravity.java (android-sdk\sources\android-22\android\view\Gravity.java)
    /** 将对象推送到其容器开始处的 x 轴位置,
        不改变它的大小。*/
        公共静态最终 int START = RELATIVE_LAYOUT_DIRECTION |重力.LEFT;
        
  • 事件#3:Toggle() 函数调用mDrawerLayout.openDrawer(Gravity.START)
    DrawerLayout.java(support-v4-22.1.1.jar 库) /*** 通过将指定的抽屉设置为不可见的动画来打开它。 ** @param 重力 Gravity.LEFT 移动左侧抽屉或 Gravity.RIGHT 为权利。 * GravityCompat.START 或 GravityCompat.END 也可以 使用。 */ 公共无效openDrawer(@EdgeGravity int重力){ 最终视图抽屉视图 = findDrawerWithGravity(gravity); if (drawerView == null) { throw new IllegalArgumentException("没有找到重力的抽屉视图" + 重力到字符串(重力)); } 打开抽屉(抽屉视图); }
  • EVENT#4:如果 DrawerLayout 的 childView 没有设置为 layout_gravity="start”left,则抛出 No drawer view found with gravity LEFT

  • 解决方案:
        <android.support.v4.widget.DrawerLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">
        <!--This will appear when the drawer is closed (default view)--> 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"> 
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:minHeight="?attr/actionBarSize"
                android:background="?attr/colorPrimary" /> 
        </LinearLayout> 
        <!-- This will appear when the drawer is opened -->
        <!-- the property,android:layout_gravity, is used internally to identify imageView as the view to be displayed when the drawer is opened -->
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height=" match_parent"
            android:layout_gravity="left"
            android:background="@drawable/img_shree_ganesha" /> 
    
        </android.support.v4.widget.DrawerLayout>
    I hope that helps. Happy Coding…
    

    【讨论】:

      【解决方案3】:

      “Android Drawer”需要是“DrawerLayout”的直接子节点。

      在您上面的示例中(re: original question example),您在“DrawerLayout”(“LinearLayout”)下只有一个直接子节点,并且在它之后没有抽屉视图

      将抽屉视图移出 LinearLayout 并将其放在其后面。

      【讨论】:

        【解决方案4】:

        在我的情况下,因为 DrawerLayout attr:tools:openDrawer="start" 我为第二个元素添加了android:layout_gravity="start"

         <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_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:openDrawer="right"
            tools:context=".map.MapFragment">
        
        <include layout="@layout/fragment_map" />
        
        <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        
        
        <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:background="@color/white"
                android:fitsSystemWindows="true"
                app:menu="@menu/drawer_view"
                app:headerLayout="@layout/drawer_nav_header"/>
        
        </android.support.v4.widget.DrawerLayout>
        

        【讨论】:

        • 正是我所缺少的。如果没有工具:openDrawer 匹配navigationView layout_gravity,我收到了原始帖子中的崩溃。
        【解决方案5】:

        添加

        android:layout_gravity="start"

        到导航视图

        喜欢这个

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

        【讨论】:

          【解决方案6】:

          您是否使用从右到左 (RTL) 布局?在 RTL 布局上设置重力会引发此异常。这可以通过设置重力开始而不是向左来解决

          【讨论】:

            【解决方案7】:

            我这样解决这个问题:

            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"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/drawer_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:layoutDirection="rtl"
                tools:openDrawer="end"
                >
            
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
            
                    <include
                        layout="@layout/toolbar_main"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
            
                    <include layout="@layout/content_main" />
            
                </LinearLayout>
            
                <com.google.android.material.navigation.NavigationView
                    android:id="@+id/navigationView"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:layoutDirection="rtl"
                    android:background="@color/white"
                    android:fitsSystemWindows="true"
                    app:headerLayout="@layout/nav_header_main"
                    app:menu="@menu/menu_drawer" />
            
            </androidx.drawerlayout.widget.DrawerLayout>
            

            在 JAVA 中:

             if (mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
                    mDrawerLayout.closeDrawer(GravityCompat.END);
                }
            

            我希望这对某人有帮助!

            【讨论】:

              【解决方案8】:

              你应该在 DrawerLayout 和 NavigationView 中使用相同的重力:例如:在 DrawerLayout 标签中使用 tools:openDrawer="right",在 NavigationView 标签中使用 android:layout_gravity="right"

              <?xml version="1.0" encoding="utf-8"?>
              <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_layout"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  tools:openDrawer="right"
                  tools:context=".map.MapFragment">
              
              <include layout="@layout/fragment_map" />
              <FrameLayout
                      android:id="@+id/content_frame"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent" />
              
              <android.support.design.widget.NavigationView
                      android:id="@+id/nav_view"
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:layout_gravity="right"
                      android:background="@color/white"
                      android:fitsSystemWindows="true"
                      app:menu="@menu/drawer_view"
                      app:headerLayout="@layout/drawer_nav_header"/>
              
              </android.support.v4.widget.DrawerLayout>
              

              【讨论】:

                【解决方案9】:

                java.lang.IllegalArgumentException: 没有找到重力向左的抽屉视图

                解决方案 如果您的抽屉布局已经有一个子视图,则将 layout_gravity = "start" 或 "left" 属性分配给您的 DrawerLayout 子视图之一。要么 只需在 DrawerLayout 视图中创建一个子视图并为其赋予 layout_gravity = "start" 或 "left" 属性。 例如

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-09-24
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-07-25
                  • 1970-01-01
                  相关资源
                  最近更新 更多