提问者:小点点

当父级设置为WRAP_CONTENT时,ViewGroup子高度测量为0


我正在创建自定义ViewGroup。如果我在没有指定LayoutParams的情况下将自定义ViewGroup添加到布局中(如下所示),它将正确显示:

...
MyCustomViewGroup myViewGroup = new MyCustomViewGroup(this);
myRelativeLayout.addView(myViewGroup);
...

如果我指定LayoutParams并将宽度设置为MATCH_PARENT,高度设置为WRAP_CONTENT(如下所示),它不会显示:

...
MyCustomViewGroup myViewGroup = new MyCustomViewGroup(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
myRelativeLayout.addView(myViewGroup, params);
...

我对这两种情况都进行了调试。

场景1
如果我没有指定任何LayoutParams,则子视图度量正确,当该方法遍历所有子视图以确定最大高度时,child.getMeasuredHeight()每次返回正确的值。

场景2
如果我指定LayoutParams,宽度为MATCH_PARENT,高度为WRAP_CONTENT,系统会进行两次传递,如下所述。

传递1
widthSpecMode=EXACTLY
width=widthSpecSize=758,这是父RelativeLayout的宽度
heightSpecMode=EXACTLY
height=heightSpecSize=1055,这是其父RelativeLlayout的高度

传递2
widthSpecMode=EXACTLY
width=widthSpecSize=758,这是父RelativeLayout的宽度
heightSpecMode=AT_MOST
然后该方法遍历所有子视图以确定最大高度,但child.getMeasuredHeight()每次返回0。

子视图是几个ImageButtons和一个TextView。它们都有内容,并且这些内容在第一个场景中显示正确。为什么它在第二个场景中显示为0高度,我该如何修复它?

''我编写了一个简化的测试应用程序来重现问题。所有必要的代码如下所示。如果有人想尝试一下,您可以简单地剪切和粘贴。

主要活动.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rootLayout = (RelativeLayout)findViewById(R.id.LayoutRoot);

        // Create an instance of MyViewGroup
        MyViewGroup viewGroupOne = new MyViewGroup(this);
        viewGroupOne.setId(1);
        rootLayout.addView(viewGroupOne);

        // Create a second instance and set layout width and height both to WRAP_CONTENT
        MyViewGroup viewGroupTwo = new MyViewGroup(this);
        viewGroupTwo.setId(2);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, 1);
        rootLayout.addView(viewGroupTwo, params);

        // Create a third Instance. Set layout width to MATCH_PARENT and height to WRAP_CONTENT
        MyViewGroup viewGroupThree = new MyViewGroup(this);
        params  = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, 2);
        rootLayout.addView(viewGroupThree, params);     
    }
}

活动_最小.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LayoutRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

</RelativeLayout>

我的视图组.java

public class MyViewGroup extends ViewGroup {

    private static int instanceCounter;

    public MyViewGroup(Context context) {
        super(context);

        instanceCounter++;

        // Add a TextView
        TextView textView = new TextView(context);
        String text = "Instance " + instanceCounter;
        textView.setText(text);
        addView(textView);

        // Add an ImageView
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(android.R.drawable.ic_menu_add);
        addView(imageView);

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int width = 0;
        int height = 0;
        int childState = 0;

        // Measure Width
        if (widthSpecMode == MeasureSpec.EXACTLY) {
            width = widthSpecSize;          
        } else {
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    measureChild(child, widthMeasureSpec, heightMeasureSpec);
                    width += child.getMeasuredWidth();
                }
            }
        }
        if (widthSpecMode == MeasureSpec.AT_MOST) {
            width = Math.min(width, widthSpecSize);
        }

        // Measure Height
        if (heightSpecMode == MeasureSpec.EXACTLY) {
            height = heightSpecSize;            
        } else {
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    height = Math.max(height, child.getMeasuredHeight());
                }
            }
        }
        if (heightSpecMode == MeasureSpec.AT_MOST) {
            height = Math.min(height, heightSpecSize);
        }

        // Combine child states
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                childState = combineMeasuredStates(childState, child.getMeasuredState());
            }
        }

        // Check against minimum width and height
        width = Math.max(width, getSuggestedMinimumWidth());
        height = Math.max(height, getSuggestedMinimumHeight());

        // Report final dimensions
        setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, childState),
                resolveSizeAndState(height, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int leftPos = 0;

        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final int childWidth = child.getMeasuredWidth();

            if (child.getVisibility() != GONE) {
                child.layout(leftPos, 0, leftPos + childWidth, getMeasuredHeight());
                leftPos += childWidth;              
            }
        }
    }
}

共1个答案

匿名用户

好的,所以你似乎忘记调用测量儿童(儿童,宽度测量规格,高度测量规格);在计算高度时