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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vmware找不到vmx文件_虚拟机重启后文件丢失

    vmware找不到vmx文件_虚拟机重启后文件丢失在使用Vmware的过程中,不小心删除了vmx文件,导致Vmware无法启动。经过上网搜查资料,找到解决办法。vmx只是一个对Vmware文件的简单描述性文件,并不包含任何实质性信息,信息主要包含在vmdk和vmxf文件中。对于Ubuntu虚拟机,用记事本创建空白文件,在其中输入下面内容并保存为ubuntu.vmx即可。(其中加粗的部分是需要修改的内容,包括vmdk文件的

    2025年6月12日
    9
  • PyCharm 配置 Git

    PyCharm 配置 Gitwindows下step1: 首先安装GitHubstep2: 找到git.exe的路径git.exe的默认路径如下:C:\Users\yourname\AppData\Local\GitHub\PortableGit_69703d1db91577f4c666e767a6ca5ec50a48d243\bin\git.exestep3: Pych

    2022年8月29日
    4
  • 新浪股票接口获取历史数据

    新浪股票接口获取历史数据这两天做了一个调用新浪股票接口获取实时以及历史股票数据的应用,因为新浪没有公开关于其接口的官方文档,所以通过各种百度差了很多关于新浪股票接口的使用,不过大家基本都是转载或者直接复制,对于实时数据的获取讲的很详细,但是缺少获取历史数据的方法。关于实时数据的获取大家可以看这篇博客:实时股票数据接口 经过不懈的努力终于再这篇博文中找到了关于新浪股票历史数据的获取方式腾讯股票接口、和讯网股票接口、新浪股票…

    2022年6月24日
    116
  • 从几个常见需求看扫描电子书处理软件选择「建议收藏」

    从几个常见需求看扫描电子书处理软件选择「建议收藏」作者:马健邮箱:stronghorse_mj@hotmail.com发布:2020.01.04这几天在eshuyuan碰到一些人谈到扫描电子书处理,很多人的习惯是使用通用图像处理软件,包括Phot

    2022年8月1日
    13
  • GoLand 2021.5.3 激活码【在线注册码/序列号/破解码】

    GoLand 2021.5.3 激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    53
  • pycharm安装包说pip版本不对_django库

    pycharm安装包说pip版本不对_django库一、pycharm安装库与pip安装库的区别项目使用哪个解释器,就用哪个解释器下的库:python安装目录解释器就用该目录下的库,项目的解释器就用项目里面的库!而pip安装的库是保存在python安装目录解释器下的。“pip成功,pycharm识别不了”,这就是因为新建项目默认解释器是用“项目的解释器”的,但是pip安装的第三方库是在python安装目录下,所以会识别不了。这里我只把我需要知道的摘下来,具体友情链接:关于pip安装第三方库,但PyCharm中却无法识别的问题;以及PyCharm安装第三

    2022年8月26日
    7

发表回复

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

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