Refreshing a LinearLayout after adding a view

后端 未结 3 791
栀梦
栀梦 2020-12-03 05:35

I\'m trying to add views dynamically to a linearlayout. I see through getChildCount() that the views are added to the layout, but even calling invalidate() on the layout doe

3条回答
  •  甜味超标
    2020-12-03 06:09

    I had the same problem and noticed that my overriden onMeasure() method wasn't called after the invalidate. So I created my own subroutine in the LinearLayout and called it before the invalidate() method.

    Here is the code for an vertical LinearLayout:

    private void measure() {
        if (this.getOrientation() == LinearLayout.VERTICAL) {
            int h = 0;
            int w = 0;
            this.measureChildren(0, 0);
            for (int i = 0; i < this.getChildCount(); i++) {
                View v = this.getChildAt(i);
                h += v.getMeasuredHeight();
                w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w;
            }
            height = (h < height) ? height : h;
            width = (w < width) ? width : w;
        }
        this.setMeasuredDimension(width, height);
    }
    

提交回复
热议问题