Android 自定义ViewGroup 设置wrap_content无效解决办法。

在自定义布局里的onmeasure 方法里:

 

private int height ;   
 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (height == 0) {
            for (int i = 0; i < getChildCount(); i++) {
                View childView = getChildAt(i);
                measureChild(childView, widthMeasureSpec, heightMeasureSpec);
                Log.e("childView", "=" + childView.getMeasuredHeight());
                height += childView.getMeasuredHeight();
            }
            Log.e("height", "=" + height);

        }
        setMeasuredDimension(widthMeasureSpec, height);
    }

设置宽高。。

 

 

 

 

你可能感兴趣的:(Android 自定义ViewGroup 设置wrap_content无效解决办法。)