为什么使用DrawerLayout给抛出:IllegalArgumentException做:DrawerLayout必须用MeasureSpec.EXACTLY
问题描述:
下测得的是我的xml:为什么使用DrawerLayout给抛出:IllegalArgumentException做:DrawerLayout必须用MeasureSpec.EXACTLY
<?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”(见navigation drawer documentation)
•抽屉视图指定dp为单位其宽度和高度父视图相匹配。抽屉宽度应不大于320dp更使用户可以随时看到的主要内容
编辑的一部分至于你的子类去,它给出了同样的错误?我猜测,当计算“wrap_content”大小时,实际宽度无效(可能为零)。您应该检查onMeasure中的宽度并确保它在限制范围内(大于零,远远小于父宽度,以便在抽屉打开时仍能看到它)。
您使用的是哪种版本的支持库? – stkent
供参考:https://android.googlesource.com/platform/frameworks/support/+/407e01608ccafe5dc24f82608583f71c34312f9c/v4/java/android/support/v4/widget/DrawerLayout.java#626不知道该库的哪个版本尽管如此。 – stkent
@stkent支持库版本26.0.0 – TomTaila