Android自定义ViewGroup遇到的问题
2018-03-23 10:57
465 查看
问题一
说明:自定义了一个ViewGroup,在onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法中去测量子view的时候使用的是:
导致添加的view的时候无聊view的layoutParams是match_parent还是wrap_content,显示的时候都是自适应内容宽度的,即使在match_parent的时候看layoutParams的确是match_parent.
所以还是仔细研究了ViewGroup的方法getChildMeasureSpec(),注意注释部分的说明.
说明:自定义了一个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-AndroidStudio(二)遇到问题,附:ScrollView中文API
- Android应用插件化开发中自定义View基础插件遇到的问题
- Android 项目需求Camera + SurfaceView实现自定义拍照,遇到的问题
- Android-Cloud-TagView-Plus集合到自定义dialog遇到的问题
- Android自定义View遇到的问题
- android自定义viewGroup 不显示问题
- Android开发中遇到的问题(一)——自定义videoview设置视频大小,实现上下黑边
- Android开发-自定义View-AndroidStudio(二)遇到问题,附:ScrollView中文API
- Android自定义View 构造方法 遇到的一些问题
- <50 android hacks>中的卡牌问题---自定义ViewGroup
- android 自定义view更新的时候遇到的问题(求解答)
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- android之自定义ViewGroup和自动换行的布局的实现
- 在Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示。
- android之自定义ViewGroup和自动换行的布局的实现
- (原创)android中使用Baidu Map遇到的问题之:移动Mapview上的放大缩小控件
- android之自定义ViewGroup和自动换行的布局的实现
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- Android自定义View研究(六)--View中的原点坐标相关问题