How to correctly size containing views
I have an Activity
that will display a custom vie开发者_C百科w made up of 2 parts. I want one part to be 1/3 of visible screen height, the other part to be 2/3.
I can override onMeasure()
and use display metrics to find the height of the display, but this does not account for the battery bar or view title sizes.
DisplayMetrics dm = new DisplayMetrics();
((WindowManager)contxt.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
How can I tell the height of the displayable area? I'm prepared to override the layout
or whatever. What is the Android
's best practices? I've seen other questions along this line, but they are inconclusive.
I figured out a way to do it. I override RelativeLayout, and get it to keep pointers to my upper and lower views. Then when it processes onMeasure, it sets the desired heights on my 2 views. They use the desired height when they handle onMeasure. This gives me one view at 2/3 of the visible area, with the other one below.
-- from my Layout class
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);
}
-- from the View derived classes
@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;
}
精彩评论