DrawerLayout必须使用MeasureSpec.EXACTLY进行测量。即使具有match_parent大小

时间:2018-10-22 15:17:05

标签: android layout drawerlayout measure

我有这个布局

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <android.support.design.widget.CoordinatorLayout
      android:id="@+id/account_menu_coordinator_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="@string/lores_ipsum"/>

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

和此通货膨胀代码:

final View content =
    (View) inflater.inflate(R.layout.activity_account_menu2, coordinatorLayout, false);

返回此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.google.android.libraries.onegoogle.demoapp, PID: 14668
    java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1053)

我看到了这个post

但是我的布局已经有match_parent。

怎么可能会出现此异常?

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题。尝试将DrawerLayout包装在约束布局中,如下所示:

 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
   ....
   </androidx.drawerlayout.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>