此方法是固定每行居中

使用
android:paddingLeft="@dimen/margin_common_10"
android:paddingRight="@dimen/margin_common_15"
进行调整左右间距
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;public class FlexBoxLayout extends ViewGroup {/*** 居中换行*/public int foxheight= 5;//换行H间隙public FlexBoxLayout(Context context) {this(context, null);}public FlexBoxLayout(Context context, AttributeSet attrs) {super(context, attrs);}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int maxWidth = MeasureSpec.getSize(widthMeasureSpec);int childCount = getChildCount();int x = 0,y = 0,row = 0,ih=0;for(int i = 0; i<childCount; i++){getChildAt(i).measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);if(getChildAt(i).getMeasuredHeight()>ih){ih = getChildAt(i).getMeasuredHeight();}}for (int index = 0; index < childCount; index++) {final View child = this.getChildAt(index);if (child.getVisibility() != View.GONE ) {child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);// 此处增加onlayout中的换行判断,用于计算所需的高度int width = child.getMeasuredWidth();int height = ih;x += width;y = row * height + height;if (x > maxWidth) {x = width;row++;ih = ih+foxheight;height = ih;y = row * (height) + height;}}}setMeasuredDimension(maxWidth, y);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {final int childCount = getChildCount();int maxWidth = r - l;int x = 0,y = 0,row = 0,ih=0;for(int i = 0; i<childCount; i++){getChildAt(i).measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);if(getChildAt(i).getMeasuredHeight()>ih){ih = getChildAt(i).getMeasuredHeight();}}for (int i = 0; i < childCount; i++) {final View child = this.getChildAt(i);if (child.getVisibility() != View.GONE ) {int width = child.getMeasuredWidth();int height = ih;x += width;y = row * height + height;if (x > maxWidth) {x = width;row++;ih = ih+foxheight;height = ih;y = row * (height) + height;}if( child.getMeasuredHeight()< ih){child.layout((x - width) + getPaddingLeft(), (y - height)+(ih-child.getMeasuredHeight())/2 ,x- + getPaddingRight(), y-(ih-child.getMeasuredHeight())/2);}else{child.layout((x - width) + getPaddingLeft(),  y - height, x + getPaddingRight(), y);}}}}}

android 自动换行布局相关推荐

  1. Android自动换行布局

    在网上找了挺久都没找到好用的自动换行布局,于是干脆自己写一个吧,原理其实就是计算布局大小,然后摆放的时候计算子view摆放的时候是否大于父view的最大宽度,超过了就在下面一行摆放就好了,注释就不多写 ...

  2. android使用桢布局,Android 常用布局

    Android的布局有: LinearLayout线性布局 RelativeLayout            相对布局 FrameLayout单桢布局 TableLayout表格布局 GridLay ...

  3. android如何看百分比版本,【JAVA】Android百分比布局

    开发我人生第一个app的时候,我在想有没有这么一种布局能适应任何手机的分辨率,今天让我们一起来看看Android 百分比布局PercentRelativeLayout. 使用这种布局首先我们要在gra ...

  4. Android开发——布局性能优化的一些技巧(一)

    0. 前言 上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高,意义在于分析了这两种布局的实现源码,算是对一个小结论的证明过程,但是对布局性能的优化效果,对这两种布 ...

  5. 浅谈Android五大布局——LinearLayout、FrameLayout和AbsoulteLa

    为什么80%的码农都做不了架构师?>>>    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了 ...

  6. android源代码居中字体,Android (布局优化) TextView实现drawable图标大小 位置与第一行文本居中...

    先看大众点评的购买须知 大众.png 如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没 ...

  7. Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分

    Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  8. Android帧布局(Frame Layout)

    Android帧布局(Frame Layout) FrameLayout是最简单的一个布局管理器.FrameLayout为每个加入其中的组件创建一个空白区域(一帧),这些组件根据layout_grav ...

  9. Android表格布局(Table Layout)

    Android表格布局(Table Layout) 先来看布局管理器之间继承关系图: 图1 可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器. 表格布局采用 ...

最新文章

  1. 整合quickx到普通cocos2dx
  2. OOJ-面向对象的JAVASCRIPT(二)
  3. 09-JS中table模糊查询
  4. Oracle恢复删除的数据
  5. C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程
  6. Java 13 新特性全面解读
  7. 如何使用TrueCrypt加密
  8. mysql上线脚本规范_专业规范的mysql启停脚本
  9. Linux连接蓝牙键盘
  10. 2019-2021届蓝桥杯——java真题集锦
  11. 程序员的外功和内功的修炼
  12. 计算机专业论文结束语,毕业设计论文的结束语
  13. 关于Android Launcher3相关优秀文章的记录
  14. PyTorch学习笔记:nn.Sigmoid——Sigmoid激活函数
  15. Zabbix5.0版本 正则表达式
  16. symbian流媒体入门之--《3gp文件格式研究》
  17. 从零开始:Python学习成长路线
  18. 多么痛的领悟~ T T
  19. fastadmin表格
  20. 机械键盘测试——《万历十五年》2

热门文章

  1. Linux服务器上部署springboot服务并测试
  2. 对Vintage未表现数据的预测方法总结
  3. 华北科技学院计算机专业录取分数线,华北科技学院2018年在河北省各专业录取分数线...
  4. ESP8266-Arduino编程实例-TEA5767收音机模块驱动
  5. 计算机专业复试线380,445名400+的科软复试线388,计算机学硕380,卷炸了
  6. [又值奥运季] 2016年里约奥运会--8月14日赛事
  7. Swift-编程一小时
  8. 直线检测——对比M-LSD直线检测(基于深度学习)与霍夫曼直线检测
  9. CF786B Legacy
  10. 【掌控板】0、基础教程