【问题标题】:android: measureChildWithMargins returns different results than view.measureandroid: measureChildWithMargins 返回与 view.measure 不同的结果
【发布时间】:2016-01-05 13:22:51
【问题描述】:

我正在 Android 中扩展 ViewGroup。目前,为了测试,我只向该视图组添加了 1 个子视图 - 通过代码实例化的 TextView。

在 ViewGroup 的 onMeasure() 中,当我调用 child.measure() 时,孩子将其高度测量为整个屏幕高度,即使在创建它时,我将 WRAP_CONTENT 作为高度布局参数传递。但是,当我使用measureChildWithMargins 测量孩子时,它会返回正确的高度。这是为什么呢?

public class CustomViewGroup extends ViewGroup {
    public CustomViewGroup (Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        // create header text view
        addView(createTitleHeader(context));
    }

    private TextView createTitleHeader(Context context) {
        TextView result = new TextView(context);
        result.setLayoutParams(new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        result.setBackgroundColor(Color.RED);
        return result;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));

        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                // the following line scales the TextView's height to the entire screen
                child.measure(widthMeasureSpec, heightMeasureSpec);
                //...while the following line works perfectly
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (changed) {
            // get view group measurements
            final int viewLeft = getPaddingLeft();
            final int viewTop = getPaddingTop();
            final int viewRight = right - left - getPaddingRight();
            final int viewBottom = bottom - top - getPaddingBottom();

            // temp rect used for measuring views
            final Rect rect = new Rect();
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    // get child params
                    final LayoutParams lp = child.getLayoutParams();
                    final int childWidth = child.getMeasuredWidth();
                    final int childHeight = child.getMeasuredHeight();

                    // calculate rect
                    rect.left = viewLeft;// + lp.leftMargin;
                    rect.top = viewTop;// + lp.topMargin;
                    rect.right = viewLeft + childWidth;// - lp.rightMargin;
                    rect.bottom = viewTop + childHeight;

                    // layout
                    child.layout(rect.left, rect.top, rect.right, rect.bottom);
                }
            }
        }
    }
}

【问题讨论】:

    标签: android viewgroup onmeasure


    【解决方案1】:

    很遗憾没有人回答您的问题。您必须调用 ViewGroup.measureChild 或 ViewGroup.measureChildWith Margins;第一的。这些是初始化视图大小的原因。

    遗憾的是,我遇到了相反的问题。 ViewGroup.measureChildWith Margins 导致我的应用崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-27
      • 2015-08-13
      • 1970-01-01
      • 2010-12-22
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多