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


相关推荐

  • 集成电路芯片半导体中英文对照术语词汇表「建议收藏」

    集成电路芯片半导体中英文对照术语词汇表「建议收藏」英语 中文 1-9   10gigabit 10Gb 1stNyquistzone 第一奈奎斯特区域 3Dfull‑waveelectromagneticsolver 3D全波电磁解算器 3-state 三态 4thgenerationsegmentedrouting 第四代分层布线技术 5Gcommercialization 5G商用 7seriesFPGA 7系列FPG

    2025年11月25日
    3
  • docker搭建LDAP统一用户认证

    docker搭建LDAP统一用户认证1 安装 LDAPdockerru dit p389 389 v data ldap ldap var lib ldap v data ldap slapd d etc ldap slapd d nameldap envLDA

    2025年7月4日
    3
  • JAVA协同过滤推荐算法

    1、什么是协同过滤在推荐系统众多方法中,基于用户的协同过滤推荐算法是最早诞生的,原理也较为简单。该算法1992年提出并用于邮件过滤系统,两年后1994年被GroupLens用于新闻过滤。一直到2000年,该算法都是推荐系统领域最著名的算法。在一个在线个性化推荐系统中,当一个用户A需要个性化推荐时,可以先找到和他有相似兴趣的其他用户,然后把那些用户喜欢的、而用户A没有听说过的物品推荐给A。…

    2022年4月7日
    54
  • oracle 11g 怎么安装,oracle 11g安装图解 Oracle 11g安装图文教程[通俗易懂]

    oracle 11g 怎么安装,oracle 11g安装图解 Oracle 11g安装图文教程[通俗易懂]之前也找过oracle11g安装图解,不过一直没找到合适的Oracle11g安装图文教程,下面这篇oracle11g安装图解比较详细,希望对Oracle学习者有帮助:一、Oracle下载注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可。路径名称中,最好不要出现中文,也不要出现空格等不规则字符。官方下地址:http://www.oracle.com/technetwo…

    2022年9月21日
    4
  • axure快速原型设计工具

    axure快速原型设计工具

    2022年2月22日
    53
  • navicate 15 激活码【2021.8最新】[通俗易懂]

    (navicate 15 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS32PGH0SQB-eyJsa…

    2022年3月26日
    241

发表回复

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

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