【已解决】 SlideMenu中OnMeasure测量两次导致Wide值不同的问题
2021-08-16 11:06:03.581 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: .............
2021-08-16 11:06:03.581 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: --------------->withSize==1080
2021-08-16 11:06:03.581 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: -------------widthMode==1073741824
2021-08-16 11:06:03.582 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: ---------------->MeasureSpec.EXACTLY=1073741824
2021-08-16 11:06:03.582 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: ---------------->MeasureSpec.UNSPECIFIED=0
2021-08-16 11:06:03.582 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: ---------------->MeasureSpec.AT_MOST=-2147483648
2021-08-16 11:06:03.582 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: --------------contentHeight ==else
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: .............
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: --------------->withSize==1890
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: -------------widthMode==1073741824
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: ---------------->MeasureSpec.EXACTLY=1073741824
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: ---------------->MeasureSpec.UNSPECIFIED=0
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: ---------------->MeasureSpec.AT_MOST=-2147483648
2021-08-16 11:06:03.585 11432-11432/com.lw.looperview D/SlideMenuView: onMeasure: --------------contentHeight ==else
2021-08-16 11:06:03.591 11432-11432/com.lw.looperview D/SlideMenuView: onLayout: ......................
我的屏幕大小是1080的 要是把参数widthMeasureSpec直接给TextView的话会超出屏幕,拿不到第一次的1080大小。
都没人回呢。
代码不给,不知道你怎么写的。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Log.d(TAG, "onMeasure: ............."); int widthSize = MeasureSpec.getSize(widthMeasureSpec); Log.d(TAG, "onMeasure: --------------->withSize=="+widthSize); int widthMode = MeasureSpec.getMode(widthMeasureSpec); Log.d(TAG, "onMeasure: -------------widthMode=="+widthMode); Log.d(TAG, "onMeasure: ---------------->MeasureSpec.EXACTLY="+MeasureSpec.EXACTLY); Log.d(TAG, "onMeasure: ---------------->MeasureSpec.UNSPECIFIED="+MeasureSpec.UNSPECIFIED); Log.d(TAG, "onMeasure: ---------------->MeasureSpec.AT_MOST="+MeasureSpec.AT_MOST); int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 测量第一个孩子,也就是内容部分 // 宽度,跟父控件一样宽。高度有三种情况,如果指定大小,那我们获取到它的大小,直接测量 // 如果是wrap_content,AT_MOST,如果是match_parent,那就给它大小。 if (firstChild == null) { return; } LayoutParams contentLayoutParams = firstChild.getLayoutParams(); int contentHeight=contentLayoutParams.height; int contentHeightMeasureSpace; if (contentHeight == LayoutParams.MATCH_PARENT) { Log.d(TAG, "onMeasure: --------------contentHeight ==MATCH_PARENT"); contentHeightMeasureSpace = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY); } else if (contentHeight == LayoutParams.WRAP_CONTENT) { Log.d(TAG, "onMeasure: --------------contentHeight ==WRAP_CONTENT"); contentHeightMeasureSpace = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST); } else { Log.d(TAG, "onMeasure: --------------contentHeight ==else"); contentHeightMeasureSpace = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY); } int a =MeasureSpec.makeMeasureSpec(1080,MeasureSpec.EXACTLY); firstChild.measure(a, contentHeightMeasureSpace); // 拿到内容部分测量以后的高度 int contentMeasuredHeight = firstChild.getMeasuredHeight(); // 测量编辑部分,宽度:3/4,高度跟内容高度一样 int editWidthSize = widthSize * 3 / 4; functionView.measure( MeasureSpec.makeMeasureSpec(editWidthSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(contentMeasuredHeight, MeasureSpec.EXACTLY) ); // 测量自己 // 宽就是前面的宽度之和,高度和内容一样 setMeasuredDimension(widthSize + editWidthSize, contentMeasuredHeight); }