DrawerLayout must be measured with MeasureSpec.EXACTLY error

北战南征 提交于 2019-11-27 14:59:36
Ismail Rubad

My DrawerLayout was inside a LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/abohawawallpapersqr"
    tools:context=".MainActivity">

        <include layout="@layout/toolbar" />

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"> 

        <!-- ...... -->
        </android.support.v4.widget.DrawerLayout>
</LinearLayout>

Then I solved the problem by changing the layout_height="wrap_content" to layout_height="match_parent" of LinearLayout.So the code is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/abohawawallpapersqr"
    tools:context=".MainActivity">

For those who still have an exception, ensure your DrawerLayout's height is match_parent

This can also happen because of the new ConstraintLayout. Make sure the drawer layout isn't in the hierarchy of ConstraintLayout.

This is a really silly bug on Android's part. I will try to file a bug for it.

Make sure your activity is not a "Dialog" activity, check in the manifest. I made this mistake and got the error.

This can also happen if you have set layout_width to wrap_content. In my case it was solved by setting width to match_parent. Hope this helps someone.

Nadun Priyankarage
requestWindowFeature(Window.FEATURE_NO_TITLE);

was solved my problem by creating the activity like below;

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}

use this ...

public class CustomDrawerLayout extends DrawerLayout {
public CustomDrawerLayout(Context context) {
       super(context);
}
public CustomDrawerLayout(Context context, AttributeSet attrs) {
       super(context, attrs);
 }
     public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(
      MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(
      MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

using Android.Support.V4.Widget; using Android.Util;

. . .

   class MyDrawerLayout: DrawerLayout
{
    public MyDrawerLayout(Context context) : base(context)
    {

    }
    public MyDrawerLayout(Context context, IAttributeSet attrs) : base(context, attrs)
    {

    }
    public MyDrawerLayout(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {

    }
    public MyDrawerLayout(IntPtr context, Android .Runtime.JniHandleOwnership jniHandleOwnership) : base(context, jniHandleOwnership)
    {

    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        widthMeasureSpec = MeasureSpec.MakeMeasureSpec(MeasureSpec.GetSize(widthMeasureSpec), MeasureSpecMode.Exactly);
        heightMeasureSpec = MeasureSpec.MakeMeasureSpec(MeasureSpec.GetSize(heightMeasureSpec), MeasureSpecMode.Exactly);

        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

    }




}

By mistake, I set a custom dialog theme to the activity instead of a custom activity theme:

activity.setTheme(R.style.StyledDialogTheme);

After i corrected this, it workes again.

Manoj Rawat

If you still get this Exception make sure that your activity is full Screen defined In Manifest u can do in Activity:

android:theme="@style/Theme.AppCompat.Light.FullScreen"

Or define this in styles.xml:

 <style name="Theme.AppCompat.Light.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

Got to Build->Clean project then Rebuild project error will be solve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!