我想出了一个办法。我重写了RelativeLayout,并让它保持指向我的上视图和下视图的指针。然后,当它处理 onMeasure 时,它会在我的 2 个视图上设置所需的高度。他们在处理 onMeasure 时使用所需的高度。这使我在可见区域的 2/3 处看到了一个视图,另一个在下面。
-- 来自我的布局类
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// set default screen heights
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if ( upper != null )
upper.setPrefHeight((heightSize*2)/3);
if ( lower != null )
lower.setPrefHeight(heightSize/3);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
-- 来自 View 派生类
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
/**
* Determines the width of this view
* @param measureSpec A measureSpec packed into an int
* @return The width of the view, honoring constraints from measureSpec
*/
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
DisplayMetrics dm = new DisplayMetrics();
((WindowManager)contxt.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
result = dm.widthPixels;
}
width = result;
return result;
}
/**
* Determines the height of this view
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
result = prefHeight;
}
height = result;
return result;
}