onMeasure里如何重置只有1个子view一行满屏, 若有多个自适应一行

这篇博客介绍了如何在onMeasure方法中处理自定义布局,使得一行最多显示4个子view,单个子view能满屏显示。当子view数量超过屏幕宽度时,自动调整子view宽度使其均匀分布。文章提供了具体的代码示例,涵盖了不同情况的处理,如一行满屏、偶数行和奇数行的子view布局以及考虑padding和margin的影响。
摘要由CSDN通过智能技术生成

onMeasure里如何重置只有1个子view一行满屏, 若有多个自适应一行

可以尝试在 onMeasure 方法中重写 measureChildWithMarginsmeasureChild 方法来实现这个需求。

对于只有一个字的 View,我们可以把它的宽度设为屏幕宽度,高度设为最大高度,这样这个 View 就会占满一整行。

而对于多个 View 自适应的情况,我们可以先测量每个 View 的宽度,并累加得到它们的总宽度。如果总宽度小于屏幕宽度,那么我们可以将 View 的宽度设为屏幕宽度除以 View 的个数,这样所有 View 的宽度都相同,就可以一行放下了。否则,我们可以默认按原有的宽度进行布局,让它们分成多行。

以下是示例代码:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    // 获取父容器的尺寸规格
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int childCount = getChildCount();
    if (childCount == 1) {  // 只有一个 View 的情况
        View child = getChildAt(0);

        // 让 View 的宽度填满屏幕,高度取最大值
        int widthMeasureSpecForChild = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        int heightMeasureSpecForChild = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        child.measure(widthMeasureSpecForChild, heightMeasureSpecForChild);

        setMeasuredDimension(widthSize, child.getMeasuredHeight());
    } else {  // 多个 View 的情况
        int totalWidth = 0;

        // 计算多个 View 的总宽度
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);

            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            int childWidth = child.getMeasuredWidth();
            totalWidth += childWidth;
        }

        // 如果总宽度小于屏幕宽度,让每一个 View 的宽度都为屏幕宽度除以 View 的个数
        if (totalWidth < widthSize) {
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);

                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize / childCount, MeasureSpec.EXACTLY);
                int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }

        // 测量自身尺寸并设置
        int totalHeight = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);

            // 测量子 View 的尺寸
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            // 计算子 View 的行宽和行高
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (lineWidth + childWidth > widthSize) {
                totalHeight += lineHeight;
                lineWidth = 0;
                lineHeight = childHeight;
            }
            lineWidth += childWidth;
            lineHeight = Ma
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值