为什么使用DrawerLayout给IllegalArgumentException:DrawerLayout必须用MeasureSpec.EXACTLY

以下是我的xml:

<?xml version="1.0" encoding="utf-8"?> <layout> <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/root_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="tailamade.boop.CustomerHomeActivity" tools:openDrawer="start"> <include layout="@layout/content_customer_home" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/navigationView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/activity_main" app:menu="@menu/menu_customer_home" /> </android.support.v4.widget.DrawerLayout> </layout> 

我也尝试编写我自己的CustomDrawerLayout像这样(没有运气):

 class CustomDrawerLayout : DrawerLayout { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { var widthMeasureSpec = widthMeasureSpec var heightMeasureSpec = heightMeasureSpec widthMeasureSpec = MeasureSpec.makeMeasureSpec( MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY) heightMeasureSpec = MeasureSpec.makeMeasureSpec( MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY) super.onMeasure(widthMeasureSpec, heightMeasureSpec) } 

}

我已经看到关于这个例外的其他职位,但“解决方案”不起作用。 这里有什么问题?

宽度需要在dpi中指定,而不是“wrap_content”(请参阅导航抽屉文档 )

•抽屉视图以dp单位指定宽度,高度与父视图匹配。 抽屉宽度不应超过320dp,用户可以随时查看主要内容的一部分

编辑至于你的孩子类,它是否给出了同样的错误? 我猜测,当“wrap_content”大小计算,实际宽度无效(可能为零)。 你应该检查onMeasure中的宽度,并确保它在限制内(大于零,远远小于父宽度,以便在抽屉打开时仍可以看到)。