2012-08-14 52 views
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是固定的宽度和高度,但这就是我想要的。

回答

1

原来我应该使用MeasureSpec.EXACTLY。不知道它存在。

child.measure(MeasureSpec.EXACTLY | childWidth, MeasureSpec.EXACTLY | childHeight); 

P.S.如果有人有更好的评论答案 - 我会接受它作为答案。我很想读一些更多的信息;)

相关问题