布局使用DrawerLayout ,出现DrawerLayout must be measured with MeasureSpec.EXACTLY.异常

布局中用DrawerLayout布局, 并且使用layout_width="match_parent",layout_height="match_parent",会出现DrawerLayout must be measured with MeasureSpec.EXACTLY.异常。

网上搜索答案无非是两种方法:

1、设置layout_width和layout_heigh时给出确切值,例如:layout_width="340dp"

2、在实际中适应各种手机屏幕大小不可能设置确切值,于是只能重写onMeasure方法:

public class MyDrawerLayout extends DrawerLayout {
    public MyDrawerLayout(Context context) {
        super(context);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDrawerLayout(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);
    }

}

 

你可能感兴趣的:(Android,Android异常)