【问题标题】:DrawerLayout inside ConstaintLayout - java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLYConstaintLayout 内的 DrawerLayout - java.lang.IllegalArgumentException:DrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量
【发布时间】:2018-03-02 16:09:50
【问题描述】:

这里是我的 xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.v4.widget.DrawerLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">    
    </android.support.v4.widget.DrawerLayout>        

</android.support.constraint.ConstraintLayout>

但是当我启动我的 Android 应用程序时,我得到了错误(在运行时):

E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:994)
     at android.view.View.measure(View.java:15848)
     at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)

【问题讨论】:

标签: android navigation-drawer android-constraintlayout


【解决方案1】:

你的问题是

java.lang.IllegalArgumentException: DrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量。

所以你必须设置高度和宽度MeasureSpec.EXACTLY

如果您在代码中使用它。高度和宽度将为MeasureSpec.EXACTLY

<android.support.v4.widget.DrawerLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

在你的代码中试试这个。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     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.support.v4.widget.DrawerLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">    
</android.support.v4.widget.DrawerLayout>

</android.support.constraint.ConstraintLayout>

【讨论】:

    【解决方案2】:

    抽屉布局必须是布局中的根元素。这就是发生错误的原因。

    此外,它应该有两个孩子 - 第一个是导航视图,第二个是包含您的内容的布局

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多