Android 探究-----继承ViewGroup后我们可以做什么?

本文详细阐述了如何在CustomView中利用自定义属性集和布局参数来实现灵活的UI布局和样式定制。通过XML配置和代码实现,展示了如何通过generateLayoutParams和checkLayoutParams方法响应自定义属性,以及在addView方法中如何合理使用setLayoutParams来优化布局性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设我们用CustomView继承ViewGroup,


在xml文件中,假设CustomView里面的23个childView,那么在生成CustomView的对象时自动回调23次父类ViewGroup的generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p);

上面的规律告诉我们,有多少个childView就调用多少次这两个方法:generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p),通过进一步研究我们发现,generateLayoutParams(AttributeSet attributeSet)方法中的attributeSet就是childView的属性集,所以如果我们要对childView加入自定义属性时,在CustomView中重写generateLayoutParams(AttributeSet attributeSet) 方法;

@Override
    public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
        return new LayoutParams(getContext(), attributeSet);
    }

上面的LayoutParams类就是CustomView的内部静态类,示例代码:

public static class LayoutParams extends ViewGroup.LayoutParams {
        private static int NO_SPACING = -1;
        private int x;
        private int y;
        private int horizontalSpacing = NO_SPACING;
        private int verticalSpacing = NO_SPACING;
        private boolean newLine = false;

        public LayoutParams(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            this.readStyleParameters(context, attributeSet);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams layoutParams) {
            super(layoutParams);
        }

        public boolean horizontalSpacingSpecified() {
            return horizontalSpacing != NO_SPACING;
        }

        public boolean verticalSpacingSpecified() {
            return verticalSpacing != NO_SPACING;
        }

        public void setPosition(int x, int y) {
            this.x = x;
            this.y = y;
        }

        private void readStyleParameters(Context context, AttributeSet attributeSet) {
            TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FlowLayout_LayoutParams);
            try {
                horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing, NO_SPACING);
                verticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_verticalSpacing, NO_SPACING);
                newLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_newLine, false);
            } finally {
                a.recycle();
            }
        }
    }

其中用到的声明自定义属性集的文件attrs.xml代码为:

<resources>
    <declare-styleable name="FlowLayout">
        <attr name="horizontalSpacing" format="dimension"/>
        <attr name="verticalSpacing" format="dimension"/>
        <attr name="orientation" format="enum">
            <enum name="horizontal" value="0"/>
            <enum name="vertical" value="1"/>
        </attr>
        <attr name="debugDraw" format="boolean" />
    </declare-styleable>
    <declare-styleable name="FlowLayout_LayoutParams">
        <attr name="layout_newLine" format="boolean"/>
        <attr name="layout_horizontalSpacing" format="dimension"/>
        <attr name="layout_verticalSpacing" format="dimension"/>
    </declare-styleable>
</resources>

当给childView加入属性后,如果childView调用getLayoutParams(),一定要记得LayoutParams lp = (LayoutParams) child.getLayoutParams();


而CustomView当通过代码去addView时,如果被add的View不调用setLayoutParams,则回调父类ViewGroup的generateLayoutParams()和checkLayoutParams(ViewGroup.LayoutParams p);如果被add的View调用setLayoutParams,则只回调父类ViewGroup的checkLayoutParams(ViewGroup.LayoutParams p);


参考:https://github.com/ApmeM/android-flowlayout


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值