在使用DrawerLayout布局时将其宽高设置为自动填充屏幕后便会出现
DrawerLayout must be measured with MeasureSpec.EXACTLY.异常。
关于这个异常,一般来说给出确切的宽高大小即可,
例如:layout_width=“340dp”
问题是很多时候没有那么合适的的时候,就只能重写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);
}
}