您的位置:首页 > 移动开发 > Android开发

Android自定义ViewGroup遇到的问题

2018-03-23 10:57 465 查看
问题一

说明:自定义了一个ViewGroup,在onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法中去测量子view的时候使用的是:

measureChildren(MeasureSpec.makeMeasureSpec(sizeWidth, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(sizeHeight, MeasureSpec.UNSPECIFIED));


导致添加的view的时候无聊view的layoutParams是match_parent还是wrap_content,显示的时候都是自适应内容宽度的,即使在match_parent的时候看layoutParams的确是match_parent.

所以还是仔细研究了ViewGroup的方法getChildMeasureSpec(),注意注释部分的说明.

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
int size = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
// 当父布局的mode为EXACTLY的时候
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
//如果子view设置了具体的数值,则子view就具有具体的数值
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
//如果子view设置了的layoutParams为match_parent,则子view就具有父布局的数值
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be bigger than us.
//如果子view设置了的layoutParams为warp_content,则子view就自适应内容大小,但最大具有父布局的数值
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent has imposed a maximum size on us
// 当父布局的mode为AT_MOST的时候
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
//如果子view设置了具体的数值,则子view就具有具体的数值
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size, but our size is not fixed.
// Constrain child to not be bigger than us.
//如果子view设置了的layoutParams为match_parent,则子view就自适应内容大小,但最大具有父布局的数值
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be bigger than us.
//如果子view设置了的layoutParams为warp_content,则子view就自适应内容大小,但最大具有父布局的数值
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent asked to see how big we want to be
// 当父布局的mode为UNSPECIFIED的时候
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let him have it
//如果子view设置了具体的数值,则子view就具有具体的数值
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should  be
//如果子view设置了的layoutParams为match_parent,则子view就自适应内容大小
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how big it should be
//如果子view设置了的layoutParams为warp_content,则子view就自适应内容大小
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
//noinspection ResourceType
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android自定义view