0
我做了一個自定義的ViewGroup,這有點不同,除了當我拖動&拖放視圖到這個ViewGroup時,編輯說它不能爲layout設置layout_width和layout_height。什麼可能導致它?孩子沒有在自定義ViewGroup中設置layout_width/-height
我只覆蓋了兩種方法 - onLayout和onMeasure。我相信我在我的onMeasure中犯了一個錯誤,因爲我還不知道它應該如何衡量孩子。
不管怎麼說,這是我的onMeasure:
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.AT_MOST);
int count = getChildCount();
for (int i = 0; i < count; ++i) {
View child = getChildAt(i);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
setMeasuredDimension(mWidth, mHeight);
}
所以,我該如何讓我的ViewGroup的孩子獲得layout_with和layout_height參數?
P.S.我知道我的ViewGroup是固定的寬度和高度,但這就是我想要的。