DrawerLayout must be measured with MeasureSpec.EXA

2019-01-12 05:29发布

I am trying to implement a Navigation drawer, but I keep getting this error. I saw the similar questions but did not work for me. I have the following layout activity_main2.xml:

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

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

   <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:background="#fff"
            android:dividerHeight="1dp" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

On my activity_main2.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    if (toolbar != null) {
        //toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }
    initDrawer();
}

private void initView() {
    leftDrawerList = (ListView) findViewById(R.id.left_drawer);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    navigationDrawerAdapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
    leftDrawerList.setAdapter(navigationDrawerAdapter);
    leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 1){
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        }
    });
}

private void initDrawer() {
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

Hopefully someone will help me with the right direction.

Many thanks in advance!

11条回答
萌系小妹纸
2楼-- · 2019-01-12 06:04

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-12 06:05

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);

    }




}
查看更多
Evening l夕情丶
4楼-- · 2019-01-12 06:06
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);
}
查看更多
神经病院院长
5楼-- · 2019-01-12 06:08

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">
查看更多
太酷不给撩
6楼-- · 2019-01-12 06:12

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.

查看更多
登录 后发表回答