我解决此问题的方法是为子视图创建自定义视图,然后覆盖自定义视图的onMeasure()。新的onMeasure()将宽度和高度设置得尽可能大。
问题是当您显示软键盘和旋转手机时。随着方向更改和键盘显示,onMeasure()将“最大”可用高度设置为可笑的小,因此当我隐藏键盘时,子视图的大小不正确。
当键盘消失时,有没有办法告诉视图重新计算布局?或者我在onMeasure()上做错了吗?这是代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
setLayoutParams(new LinearLayout.LayoutParams(
measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec))
);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
Display display = ( (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenWidth = display.getWidth();
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the view
result = screenWidth;
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
Log.d(TAG, "Width: "+String.valueOf(result));
return result;
}
measureHeight()以同样的方式完成。
答案 0 :(得分:0)
你的super.onMeasure(widthMeasureSpec,heightMeasureSpec)使用传入的值,我认为你希望这些值是实际测量的宽度和高度:
super.onMeasure(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));