【问题标题】:ViewGroup child heights measure 0 when parent set to WRAP_CONTENT当父级设置为 WRAP_CONTENT 时,ViewGroup 子级高度测量为 0
【发布时间】:2014-01-04 07:22:07
【问题描述】:

我正在创建一个自定义 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,则系统会执行两次通过 onMeasure 的操作,如下所述。

通过 1
widthSpecMode = 完全正确
width = widthSpecSize = 758 这是父RelativeLayout的宽度
heightSpecMode = 完全正确
height = heightSpecSize = 1055 也就是父RelativeLayout的高度

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

子视图是几个 ImageButton 和一个 TextView。它们都有内容,并且该内容在第一个场景中正确显示。 为什么在第二种情况下它显示为 0 高度,我该如何解决?

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

MainActivity.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);     
    }
}

activity_main.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>

MyViewGroup.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;              
            }
        }
    }
}

【问题讨论】:

  • 发布 MyCustomViewGroup.onLayout
  • @pskink 再次嗨。谢谢你看。我已按要求更新了我的帖子。
  • 很抱歉,但由于我看到相反的结果,所以帮不上忙:wirhout params 我什么都看不到,使用 params wrap_content, weap_content 我看到了一些东西
  • 但总的来说,你不应该在 onMeasure 中测量()你所有的孩子吗?
  • @pskink 是的,我应该这样做。这个 ViewGroup 最终将在其他位置添加许多其他子视图。然而,首先我只是在标题栏上工作,它将在 ViewGroup 顶部的一行中包含几个 ImageButtons 和一个 TextView。但是为了避免任何混淆,我现在编写了一个没有这种复杂性的小测试应用程序。我会发布这个来代替旧的和误导性的代码。

标签: android viewgroup onmeasure


【解决方案1】:

好吧,看来你忘了调用 measureChild(child, widthMeasureSpec, heightMeasureSpec);在onMeasure中计算高度时

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多