onMeasure在我的自定义视图组android中没有被调用

Im having two custom viewgroups, superViewGroup and subViewGroup. The subviewgroup contains views. Im adding my superviewgroup to a linearLayout and the subViewGroups to my superviewgroup.

The superviewgroup onMeasure() is getting called but not in the subviewgroup. but in both cases onLayout() method is getting called.

The code as follows

public class SuperViewGroup extends ViewGroup{

    public SuperViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        final int count = getChildCount();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                child.layout(0, 0, getWidth(), getHeight());

            }
        }


    }


}


public class SubViewGroup extends ViewGroup{

    public SubViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("boxchart","INSIDE ON MEASURE SUB VIEWGROUP");
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        final int count = getChildCount();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                child.layout(0, 0, getWidth(), getHeight());

            }
        }


    }


}

Comments are appreciated. thanks in advance.

#0

Because you have to actually pass the measure to the children views:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
    final int count = getChildCount();

    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            //Make or work out measurements for children here (MeasureSpec.make...)
            measureChild (child, widthMeasureSpec, heightMeasureSpec)
        }
    }
}

Otherwise you never actually measure your children. It is up to you to decide how to do this. Just because your SuperViewGroup is in a linear layout, your SuperViewGroup takes on responsibility to measure its children.

推荐文章

如何在flash上合成准确的频率?

如何在flash上合成准确的频率?

推荐文章

Rails设计,如何解密密码?

Rails设计,如何解密密码?

推荐文章

是否可以使用install shell脚本设置路径?

是否可以使用install shell脚本设置路径?

推荐文章

如何使JTable单元格中的JButton可单击?

如何使JTable单元格中的JButton可单击?

推荐文章

“社交网络”编程难题

“社交网络”编程难题

推荐文章

从活动目录中获取任何用户的类型

从活动目录中获取任何用户的类型

推荐文章

SQL正在破坏一个我没有输入代码的字符

SQL正在破坏一个我没有输入代码的字符

推荐文章

sql查询逻辑-sql server 2008

sql查询逻辑-sql server 2008

推荐文章

iPython从函数调用中清除缓存?

iPython从函数调用中清除缓存?

推荐文章

基于url动态更改site.master页面

基于url动态更改site.master页面

推荐文章

我正确使用这些指针了吗?我分配了一些东西,但之后马上就失效了

我正确使用这些指针了吗?我分配了一些东西,但之后马上就失效了

推荐文章

防止在页面加载时多次调用jQuery函数

防止在页面加载时多次调用jQuery函数

推荐文章

向tabBarController添加tap手势

向tabBarController添加tap手势

推荐文章

在包含许多对象的图像中查找对象(Python)

在包含许多对象的图像中查找对象(Python)

推荐文章

WPF ValidationRule绑定正在引发XMLParseException

WPF ValidationRule绑定正在引发XMLParseException

推荐文章

没有从GridView返回结果。SelectedRow.Cells(x).Text?

没有从GridView返回结果。SelectedRow.Cells(x).Text?