【问题标题】:Why does using a DrawerLayout give IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY为什么使用 DrawerLayout 会产生 IllegalArgumentException: DrawerLayout must be measure with MeasureSpec.EXACTLY
【发布时间】:2017-08-09 00:09:53
【问题描述】:

以下是我的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)
}

}

我看过其他有关此异常的帖子,但“解决方案”不起作用。这里有什么问题?

【问题讨论】:

标签: android kotlin drawerlayout


【解决方案1】:

宽度需要在dpi中指定,而不是“wrap_content”(见navigation drawer documentation

•抽屉视图以 dp 为单位指定其宽度,高度与父视图相匹配。抽屉宽度不应超过 320dp,以便用户始终可以看到主要内容的一部分

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    相关资源
    最近更新 更多