小工具知识网

当前位置: 首页 > 行业知识 > Android UI 代码实现:可换行的布局控件

行业知识

Android UI 代码实现:可换行的布局控件

2026-11-25 00:18:43 来源:互联网转载

文章目录

    • ShifterLayout:子控件的宽度不固定
    • ShifterLayout2:子控件的宽度一致且固定

    ShifterLayout:子控件的宽度不固定

    /** * 可以换行的布局控件,其中的子控件宽度不固定 */public class ShifterLayout extends ViewGroup {    public ShifterLayout(Context context) {        super(context);    }    public ShifterLayout(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        int childTop = this.getPaddingTop();        int childLeft = this.getPaddingLeft();        int childWidth = 0;        int childHeight  = 0;        int tempChildLeft = 0;        for(int i=0; i= getMeasuredWidth()) {                    childLeft = this.getPaddingLeft();                    childTop += childHeight + lp.bottomMargin+lp.topMargin;                }                //                child.layout(childLeft , childTop,                        childLeft+childWidth, childTop+ childHeight);                childLeft += childWidth+lp.rightMargin+lp.leftMargin;            }        }    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int childTop = this.getPaddingTop();        int childLeft = this.getPaddingLeft();        int childWidth = 0;        int childHeight  = 0;        int height = 0;        int tempChildLeft = 0;        int width = MeasureSpec.getSize(widthMeasureSpec);        for(int i=0; i= width) {                    childLeft = this.getPaddingLeft();                    childTop += childHeight + lp.bottomMargin+lp.topMargin;                }                childLeft += childWidth+lp.rightMargin+lp.leftMargin;             }        }        height = childTop + childHeight + this.getPaddingBottom();        setMeasuredDimension(width, height);    }    @Override    public LayoutParams generateLayoutParams(AttributeSet attrs) {        return new MarginLayoutParams(getContext(), attrs);    }}

    ShifterLayout2:子控件的宽度一致且固定

    /** * 可以换行的布局控件,其中的子控件宽度相同且固定 */public class ShifterLayout2 extends ViewGroup {    private int childWidth, childHeight;    public ShifterLayout2(Context context) {        super(context);    }    public ShifterLayout2(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {//        super.onLayout(changed, l, t, r, b);        int childTop = this.getPaddingTop();        int childLeft = this.getPaddingLeft();        int tempChildLeft = 0;        for(int i=0; i= getMeasuredWidth()) {                    childLeft = this.getPaddingLeft();                    childTop += childHeight + lp.bottomMargin+lp.topMargin;                }                //                child.layout(childLeft , childTop,                        childLeft+childWidth, childTop+ childHeight);                childLeft += childWidth+lp.rightMargin+lp.leftMargin;            }        }    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        View childView = getChildAt(0);        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);        //其他类想要获取布局控件中的子控件的宽高:布局控件.getChildAt(0).getMeasuredWidth/Height()        //不measure其他子控件,目前没有遇到问题        measureChildWithMargins(childView, widthMeasureSpec, 0, heightMeasureSpec, 0);//        for(int i=0; i

上一篇:山东网络推广公司助力企业网络营销,塑造品牌魅力!

下一篇:三丰云服务器怎么调