zoukankan      html  css  js  c++  java
  • Android下自动折行 效果

    关于自动折行,网上面已经有很多实现代码了,我在之前也实现过,最近再次用上这个效果,发现之前自己写的代码很丑陋,于是进行了重构,现在把代码放出来。

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.LinearLayout;
    
    
    public class FlowLinearLayout extends LinearLayout {
        public FlowLinearLayout(Context context) {
            super(context);
        }
    
        public FlowLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
    
            int childCount = getChildCount();
            int measuredX = 0;
            int measuredY = 0;
            int raw = 1;
            int maxRowItemHeight = 0;
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                if (child.getVisibility() == View.GONE) {
                    return;
                }
                child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                if (height > maxRowItemHeight) {
                    measuredY = getMeasuredY(measuredY, raw, maxRowItemHeight, height);
                    maxRowItemHeight = height;
                }
                measuredX += width;
                if (measuredX > maxWidth) {
                    measuredX = width;
                    raw++;
                    measuredY += height;
                    maxRowItemHeight = 0;
                }
            }
            measuredY += getPaddingTop() + getPaddingBottom();
            setMeasuredDimension(maxWidth, measuredY);
        }
    
        private int getMeasuredY(int measuredY, int raw, int maxRowItemHeight, int height) {
            if (raw == 1) {
                measuredY = height;
            } else {
                measuredY += height - maxRowItemHeight;
            }
            return measuredY;
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int rightBorder, int b) {
    
            int childCount = getChildCount();
            int maxRowItemHeight = 0;
            int measuredX = getPaddingLeft();
            int measuredY;
            int raw = 1;
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                if (child.getVisibility() == View.GONE) {
                    return;
                }
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                if (height > maxRowItemHeight) {
                    maxRowItemHeight = height;
                }
                measuredX += width;
                if (measuredX > rightBorder) {
                    measuredX = getPaddingLeft() + width;
                    raw++;
                }
                measuredY = getPaddingTop() + raw * maxRowItemHeight;
                child.layout(measuredX - width, measuredY - maxRowItemHeight, measuredX,
                        measuredY);
            }
        }
    
    }
    

    总共代码100行,实现了显示效果如下图

      

    小遗憾是没有写magin,所以子元素很紧凑,大家可以自己写一个magrin参数,添加上对于的效果。

  • 相关阅读:
    mooc-IDEA 项目/文件之间跳转--002
    003--PowerDesigner创建索引与外键
    002--PowerDesigner显示注释comment
    001--PowerDesigner连接MySQL
    如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......
    eas之如何获取当前用户
    eas之f7
    eas之打开窗体
    eas之怎么设置单据保存或者提交完不跳到下个新增页面
    eas之EntityViewInfo对象mainQuery中查询条件
  • 原文地址:https://www.cnblogs.com/sunzhuo1228/p/4902798.html
Copyright © 2011-2022 走看看