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

标签 android viewgroup onmeasure

我正在创建自定义 ViewGroup。 如果我将我的自定义 ViewGroup 添加到布局而不指定 LayoutParams(如下所示),它会正确显示:

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

我已经为这两种情况运行了调试。

场景一
如果我没有指定任何 LayoutParams, subview 会正确测量,并且当该方法遍历所有 subview 以确定最大高度时,child.getMeasuredHeight() 每次都会返回正确的值。

场景 2
如果我将 LayoutParams 的宽度指定为 MATCH_PARENT,将高度指定为 WRAP_CONTENT,系统将执行两次 onMeasure,如下所述。

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

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

subview 是几个 ImageButtons 和一个 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;              
            }
        }
    }
}

最佳答案

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

关于android - 当父级设置为 WRAP_CONTENT 时,ViewGroup 子级高度测量为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918222/

相关文章:

java - 如何强制派生类调用 super 方法? (就像安卓一样)

java - Android - 使用 ViewGroup 的 NullPointerException

android - 如何在 Viewgroup 类中正确覆盖 onLayout 方法

android - 3 个 textView 项目根据其长度换行

android - onMeasure() 方法的 View 和 View 组之间有什么区别?

android - 制作动态启动器图标

java - 无法解析符号 'ButterKnife'

安卓图库

android - 如何从 ListAdapter 正确获取 View ?