• 用ViewGroup实现多View自动换行的功能


    public class PredicateLayout extends ViewGroup {
        private int line_height;
        public static final int DEFAULT_HORIZONTAL_SPACING = 5;
        public static final int DEFAULT_VERTICAL_SPACING = 5;
    
        private int horizontalSpacing;
        private int verticalSpacing;
    
        public PredicateLayout(Context context) {
            super(context);
        }
    
        public PredicateLayout(Context context, int horizontalSpacing, int verticalSpacing) {
            super(context);
            this.horizontalSpacing = horizontalSpacing;
            this.verticalSpacing = verticalSpacing;
        }
    
        public PredicateLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.PredicateLayout);
            horizontalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.PredicateLayout_item_h_space, DEFAULT_HORIZONTAL_SPACING);
            verticalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.PredicateLayout_item_v_space, DEFAULT_VERTICAL_SPACING);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
    
            final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
            int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
            final int count = getChildCount();
            int line_height = 0;
            // int line_height = 22;
    
            int xpos = getPaddingLeft();
            int ypos = getPaddingTop();
    
            for (int i = 0; i < count; i++) {
                final View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    child.measure(MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED));
    
                    final int childw = child.getMeasuredWidth();
                    line_height = Math.max(line_height, child.getMeasuredHeight() + verticalSpacing);
    
                    if (xpos + childw > width) {
                        xpos = getPaddingLeft();
                        ypos += line_height;
                    }
    
                    xpos += childw + horizontalSpacing;
                }
            }
            this.line_height = line_height;
    
            if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
                height = ypos + line_height;
    
            } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
                if (ypos + line_height < height) {
                    height = ypos + line_height;
                }
            }
            setMeasuredDimension(width, height);
        }
    
        @Override
        protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
            return new LayoutParams(1, 1); // default of 1px spacing
        }
    
        @Override
        protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
            return p instanceof LayoutParams;
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            final int count = getChildCount();
            final int width = r - l;
            int xpos = getPaddingLeft();
            int ypos = getPaddingTop();
    
            for (int i = 0; i < count; i++) {
                final View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    final int childw = child.getMeasuredWidth();
                    final int childh = child.getMeasuredHeight();
                    // final int childh = 24;
                    if (xpos + childw > width) {
                        xpos = getPaddingLeft();
                        ypos += line_height;
                    }
                    child.layout(xpos, ypos, xpos + childw, ypos + childh);
                    xpos += childw + horizontalSpacing;
                }
            }
        }
    
    }
  • 相关阅读:
    SiteMesh在项目中的配置
    javascript深入理解js闭包
    小tip: 使用CSS将图片转换成黑白(灰色、置灰)
    java程序员最不愿意看到的十件事
    Spring AOP AspectJ
    2万字Java并发编程面试题整理(含答案,建议收藏)
    85道Java微服务面试题整理(助力2020面试)
    10个很多人不知道的Redis使用技巧
    2020年大厂Java面试题(基础+框架+系统架构+分布式+实战)
    2020年薪30W的Java程序员都要求熟悉JVM与性能调优!
  • 原文地址:https://www.cnblogs.com/lilihuang/p/2615346.html
Copyright © 2020-2023  润新知