我写了一个非常简单的视图组,扩展了LinearLayout,如下所示。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
ViewParent viewParent = getParent();
if(viewParent instanceof RelativeLayout)
{
RelativeLayout parentLayout = (RelativeLayout)viewParent;
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Log.d(Constants.TAG, "Parent height is " + parentLayout.getMeasuredHeight());
Log.d(Constants.TAG, "Parent height is1 " + parentLayout.getHeight());
int height = parentLayout.getMeasuredHeight();
// getChildAt(0).measure(MeasureSpec.EXACTLY | AndroidUtilities.dp(70), MeasureSpec.EXACTLY | (int)(parentLayout.getMeasuredHeight()*0.6));
// getChildAt(1).measure(MeasureSpec.EXACTLY | AndroidUtilities.dp(70), MeasureSpec.EXACTLY | (int)(parentLayout.getMeasuredHeight()*0.4));
getChildAt(0).measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(70), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int)(height* 0.6), MeasureSpec.EXACTLY));
getChildAt(1).measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(70), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int)(height* 0.6), MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize, parentLayout.getMeasuredHeight());
}
此视图组的父级是RelativeLayout
,其中包含一个简单的TextView。
在Android 4.2.2上,parentLayout.getMeasuredHeight()返回16777215.我无法理解这个数字。它适用于更高版本的Android版本。
有没有人遇到过这个?
答案 0 :(得分:1)
我还没有看到这个,但是文档中有关于适合您情况的错误的说明:
至于16777215(等于2 ^ 24 - 1),我不确定,但它似乎是一个非常大的巧合,它等于MEASURED_SIZE_MASK
。参见
http://developer.android.com/reference/android/view/View.html#MEASURED_SIZE_MASK