Android 自定义流式布局

Android 自定义流式布局自定义流式布局自定义流式布局 处理 margin 值 支持 addView 和 XML 布局 代码实现 publicclassT privatefinal ChildInfo childrenInfo newArrayList lt gt publicTagLay Contextconte super context pub ChildInfo

自定义流式布局

自定义流式布局,处理margin值,支持addView和XML布局。

在这里插入图片描述

代码实现

public class TagLayout extends ViewGroup { 
    private int horizontalSpace = dp2px(20); private int verticalSpace = dp2px(16); //所有子View private ArrayList<ArrayList<View>> allViewList = new ArrayList<>(); //存储每一行高度 private ArrayList<Integer> lineHeightList = new ArrayList<>(); public TagLayout(Context context) { 
    super(context); } public TagLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); } public TagLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    allViewList.clear(); lineHeightList.clear(); //获取父View的模式和尺寸 int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); //定义TagLayout已使用宽和高空间 int widthUsed = 0; int heightUsed = 0; //定义单行的宽的已使用空间 int lineWidthUsed = 0; //定义单行最大值 int lineMaxHeight = 0; //一行的子View ArrayList<View> lineViews = new ArrayList<>(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { 
    View childView = getChildAt(i); measureChildWithMargins(childView, widthMeasureSpec, 0, heightMeasureSpec, heightUsed); //换行 if (widthMode != MeasureSpec.UNSPECIFIED && getPaddingLeft() + getPaddingRight() + lineWidthUsed + childView.getMeasuredWidth() + horizontalSpace > widthSize) { 
    allViewList.add(lineViews); lineHeightList.add(lineMaxHeight); lineViews = new ArrayList<>(); widthUsed = Math.max(widthUsed, lineWidthUsed); heightUsed += lineMaxHeight + verticalSpace; lineWidthUsed = 0; lineMaxHeight = 0; measureChildWithMargins(childView, widthMeasureSpec, 0, heightMeasureSpec, heightUsed); } lineViews.add(childView); lineWidthUsed += childView.getMeasuredWidth() + horizontalSpace; lineMaxHeight = Math.max(lineMaxHeight, childView.getMeasuredHeight()); //处理最后一行 if (i == childCount - 1) { 
    allViewList.add(lineViews); lineHeightList.add(lineMaxHeight); widthUsed = Math.max(widthUsed, lineWidthUsed); heightUsed += lineMaxHeight; } } int selfWidth = widthUsed + getPaddingLeft() + getPaddingRight(); int selfHeight = heightUsed + getPaddingTop() + getPaddingBottom(); setMeasuredDimension(selfWidth, selfHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    int lineCount = allViewList.size(); int left = getPaddingLeft(); int top = getPaddingTop(); for (int i = 0; i < lineCount; i++) { 
    ArrayList<View> lineViewList = allViewList.get(i); for (View childView : lineViewList) { 
    int childLeft = left; int childTop = top; int childRight = childLeft + childView.getMeasuredWidth(); int childBottom = childTop + childView.getMeasuredHeight(); childView.layout(childLeft, childTop, childRight, childBottom); left = childRight + horizontalSpace; } int lineHeight = lineHeightList.get(i); top += lineHeight + verticalSpace; left = getPaddingLeft(); } } @Override protected LayoutParams generateDefaultLayoutParams() { 
    return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { 
    return new MarginLayoutParams(getContext(), attrs); } @Override protected LayoutParams generateLayoutParams(LayoutParams p) { 
    return new MarginLayoutParams(p); } @Override protected boolean checkLayoutParams(LayoutParams p) { 
    return p instanceof MarginLayoutParams; } public int dp2px(float dpValue) { 
    float scale = Resources.getSystem().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } } 

代码下载

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233353.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号