此方法是固定每行居中

使用
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. 原生javascript
  2. 经验:在mysql中避免重复插入数据的4种方式
  3. 变声原理:卷积和传递函数
  4. CentOS6.5系统重启后宕机
  5. STL之函数对象和谓词
  6. 为什么引入Memcached?
  7. 发卡源码php免签约,2021亲测响应式PHP个人自动发卡源码-AZ发卡网源码 对接免签约易支付...
  8. python怎样打开加密的文件_如何使用python加密多个文件
  9. LeetCode Week 4:第 31 ~ 40 题
  10. JAVA:json解析
  11. 【AR】DroidCam笔记本调用手机摄像头(smartphone‘s camera as pc webcam)
  12. 计算机中丢失audiodsp,AudioDsp.dll(缺失AudioDsp.dll文件修复工具)V1.0 免费版
  13. OSPF网络类型以及不规则区域练习
  14. kprobe与perf probe使用简单记录
  15. 华科计算机学院专业课,华科计算机考研专业课有哪些
  16. c语言如何编程硬件,很多硬件开发都用C语言编程.PPT
  17. 小重山 2022年4月22日
  18. Java Poi word 插入图片并添加边框
  19. RFID技术给图书馆带来的优势
  20. 《您的设计模式》(CBF4LIFE)之“状态模式”【整理】

热门文章

  1. 巴渝工匠杯html5,巴渝工匠杯重庆市第十二届高职院校.PDF
  2. 网站建设之优质软文“赢”销不可或缺
  3. openpyxl 读写excel2007使用笔记【感谢鱿鱼哥】
  4. 中文新闻分类 数据集_最新开源LiDAR数据集LSOOD:四种常见的室外物体分类
  5. h2兼容mysql_H2数据库攻略
  6. 做奥运志愿者其实很光荣的
  7. 关键字 常量 (注意事项)
  8. protocal-buffers C++基础
  9. js sha256加密和java,在C#中的sha256加密和js中的sha256加密结果不同
  10. matlab函数大全