DrawerLayout必须MeasureSpec.EXACTLY误差测量 [英] DrawerLayout must be measured with MeasureSpec.EXACTLY error

查看:249
本文介绍了DrawerLayout必须MeasureSpec.EXACTLY误差测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个抽屉式导航栏,但我不断收到此错误。
我看到了类似的问题,但对我没有工作。我有以下布局activity_main2.xml:

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>

在我的activity_main2.java

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.

提前感谢!

推荐答案

我DrawerLayout是里面的LinearLayout:

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>

然后,我通过更改 layout_height =WRAP_CONTENT layout_height =match_parent的解决了这个问题的LinearLayout 。所以code是:

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">

这篇关于DrawerLayout必须MeasureSpec.EXACTLY误差测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆