转载请标明出处:http://blog.csdn.net/yansong_post ,本文出自【李岩松的博客】

1.概述

本篇给大家带来一个实例,FlowLayout,什么是FlowLayout,我们常在App 的搜索界面看到热门搜索词,就是FlowLayout,我们要实现的就是图中的效果,就是根据容器的宽,往容器里面添加元素,如果剩余的控件不足时候,自行添加到下一行,FlowLayout也叫流式布局,在开发中还是挺常用的.
2.对所有的子View进行测量
onMeasure方法的调用次数是不确定的,所以为了避免测量出错,需要把总的List集合,清空一下,一个View的绘制,需要经过onMeasure方法的测量,和onLayout方法的排版才能显示出来,在测量的方法中,我们把该ViewGroup中的所有子View遍历出来,添加到一行中的List集合中,再把一行中的所有的元素集合添加到总的集合中去,并对每个子View元素进行测量,测量的参数,我们给0,或者未指定,,如果不是一行中的第一元素,并且通过 getUsablewWidth()方法获取一行中可用的宽度,不够容纳下一元素,时就新创建一个集合,来装一行中所有元素,再把所有的子View元素全部测量完成后,我们还需要通过setMeasuredDemoetion()方法把测量出来的宽和高保存起来,保存之后可以调用getMeasureWidth获取测量之后的宽了.
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {allLines.clear();//测量容器的宽和高int containerMeasuredWidth = MeasureSpec.getSize(widthMeasureSpec);//这个集合用于保存单行ArrayList<View> oneLine = null;for (int i = 0; i < getChildCount(); i++) {//获取每一ChiledviewView child = getChildAt(i);int UnspecifiedMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);child.measure(UnspecifiedMeasureSpec, UnspecifiedMeasureSpec);//相当于传了一个0,0;//如果是第1个view就new一个新行出来,或者View大于了可用的宽度,if (i == 0 || child.getMeasuredWidth() > getUsablewWidth(containerMeasuredWidth, oneLine,oneLine.size())) {oneLine = new ArrayList<View>();allLines.add(oneLine);}oneLine.add(child);}int lineNumber = allLines.size();int allLinesHeight = getChildAt(0).getMeasuredHeight() * lineNumber;int verticalTotalpadding = getPaddingBottom() + getPaddingTop();//垂直总的spcingint verticalTotalSpcing = 8 * (lineNumber - 1);//容器的高 = 所有View的高 + 垂直方向的Padding + 垂直总的spcingint containerMeasureHeight = allLinesHeight + verticalTotalpadding + verticalTotalSpcing;setMeasuredDimension(containerMeasuredWidth, containerMeasureHeight);}

3.获取一行中可用的空间

获取一行中可用的宽度,需要我们传入容器的宽度,和一行元素的集合,和元素之间的间隔,,然后遍历所有的元素,通过一个变量来保存所有View测量出来宽度的总和,用容器的宽 减去,子View宽度的总和减去水平方向的间隔,以及左右两边的Padding,得到一行中可用的宽度
private int getUsablewWidth(int containerMeasuredWidth, ArrayList<View> oneLine,int needSpacingCount) {int oneLineWidth = 0;for (View view : oneLine) {oneLineWidth += view.getMeasuredWidth();}//水平方向两边的paddingint horizotalPadding = getPaddingLeft() + getPaddingRight();int horizontalTotalSpcing = horizotalPadding * needSpacingCount;int usablewWidth = containerMeasuredWidth - oneLineWidth - horizotalPadding - horizontalTotalSpcing;return usablewWidth;}

3.对所有的子View进行排版

还是遍历每一行中的每一个元素,对该元素执行排版方法,通过child.getMeasuredWidth();和child.getMeasuredHeight();获取测量后的View的宽和高,通过child.layout(l,t,r,b),对View进行位置的摆放,left就是上个元素的Rigth,Top,就是上一行元素的Bootom,Rigth就是Left+View自身的宽度,Bottom是Top+View自身的高度,最后,因为我们手动把TextView的宽改变了,跟测量时的宽不一样了,重新调用测量即可
 protected void onLayout(boolean changed, int l, int t, int r, int b) {int tempRight = 0;//保存一行中上一个View的Rightint tempBottom = 0;//保存上一行View的Bottom位置///遍历第一排for (int row = 0; row < allLines.size(); row++) {ArrayList<View> oneLines = allLines.get(row);//计算一行中每个Veiw可以分到的平均宽度int  totalUsableWidth= getUsablewWidth(getMeasuredWidth(), oneLines,oneLines.size()-1);int averageUsablewWidth =  totalUsableWidth/oneLines.size();//遍历的是一行的内容for (int column = 0; column < oneLines.size(); column++) {View child = oneLines.get(column);//获取测量的宽高int measuredWidth = child.getMeasuredWidth();int measuredHeight = child.getMeasuredHeight();//如果是一行中的第一个View则排在第0个位置int left = column == 0 ? getPaddingLeft() : tempRight + 8;//如果是第1行Top坐标是PaddingTop的位置,否则就上一个View的bottom位置int top = row == 0 ? getPaddingTop() : tempBottom + 8;int right = left + measuredWidth ;//+ averageUsablewWidth;int bootom = top + measuredHeight;child.layout(left, top, right, bootom);tempRight = right;int WidthMeasureSpec = MeasureSpec.makeMeasureSpec(child.getWidth(), MeasureSpec.EXACTLY);int HeightMakeMeasureSpec = MeasureSpec.makeMeasureSpec(child.getHeight(), MeasureSpec.EXACTLY);child.measure(WidthMeasureSpec,HeightMakeMeasureSpec);}tempBottom = oneLines.get(0).getBottom();}}
4.Activity
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);FlowLayout flowLayout = new FlowLayout(this);flowLayout.setPadding(6, 6, 6, 6);for (String text : list) {TextView textView = new TextView(this);textView.setBackgroundResource(R.drawable.bg_text);textView.setGravity(Gravity.CENTER);textView.setPadding(6, 6, 6, 6);textView.setText(text);textView.setTextSize(20);flowLayout.addView(textView);}setContentView(flowLayout);}   }

5.TextView 的背景

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><stroke android:width="1dp"android:color="#5000" /><corners android:radius="6dp"/>
</shape>

Android自定ViewGroup实现流式布局相关推荐

  1. Android 中自定义ViewGroup实现流式布局的效果

    博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,

  2. Android 自定义UI 实战 02 流式布局

    Android 自定义UI 实战 02 流式布局-- 自定义ViewGroup 第二章 自定义ViewGroup 流式布局 文章目录 Android 自定义UI 实战 02 流式布局-- 自定义Vie ...

  3. Android自定义View之实现流式布局

    Android自定义View之实现流式布局 运行效果 流式布局 把子控件从左到右摆放,如果一行放不下,自动放到下一行 自定义布局流程 1. 自定义属性:声明,设置,解析获取自定义值 在attr.xml ...

  4. android实现标签功能,Android实现热门标签的流式布局

    一.概述: 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出) 类似的 ...

  5. android自定义view流布局,Android控件进阶-自定义流式布局和热门标签控件

    一.概述: 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧 类似的自定义换行流式布局控件.下 ...

  6. android 搜索历史流布局,FlowLayout流式布局实现搜索清空历史记录

    本文实例为大家分享了FlowLayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下 效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中 ...

  7. 自定义 FlowLayout流式布局搜索框 加 GreenDao存取搜索记录,使用RecyclerView展示

    输入框布局的shape <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android ...

  8. android 仿快递步骤_【干货速递,建议收藏】Android 流式布局,一篇搞定

    今天我们来看Android的流式布局. 所谓流式布局指的是ViewGroup中同一行的宽度不足以容纳下一个子view时,进行换行处理,而不需要考虑子view的大小,每一行的高度以其中最高者为准. Ta ...

  9. Android自定义ViewGroup的布局,往往都是从流式布局开始

    前言 前面几篇我们简单的复习了一下自定义 View 的测量与绘制,并且回顾了常见的一些事件的处理方式. 那么如果我们想自定义 ViewGroup 的话,它和自定义View又有什么区别呢?其实我们把 V ...

  10. Android自定义控件之流式布局

    效果图: 一.首先创建我 们的自定义流式布局 public class FlowLayoutView extends ViewGroup {public FlowLayoutView(Context ...

最新文章

  1. 16道嵌入式C语言面试题
  2. 【数据竞赛】百赛百试,十试九灵的特征筛选策略-Pearson Correlation
  3. SAP面向iOS设备推Cloud Platform SDK工具
  4. 中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-接收和发送数据)
  5. mysql 的命名管道_SQL Server中的命名管道(named pipe)及其使用
  6. git 撤销全部的commit_git如何撤销commit的方法(未push)
  7. 修改ElementUI样式的几种方式
  8. 【java笔记】File类(2):获取,判断,创建,删除,遍历目录方法
  9. BERT4Rec:知道用户的播放(购买、点击、...)序列 item1, item2, item3,预测下一个播放的item问题。
  10. Python的发展前景在哪?怎么样让Python程序员持续发展?
  11. java项目集成J2Cache(一级缓存ehCache,二级缓存redis)
  12. DWG文件打开乱码怎么办?
  13. 新宝资讯3000亿资金候场A股
  14. 用GoldWave和剪映简单编辑视频
  15. ui设计学习路线图分享送给初学者
  16. Nessus 安装文件和详细教程(kali系统,附网盘下载链接)
  17. 考试系统mysql数据库设计_驾校理论考试系统之数据库设计一
  18. 什么是禅?《禅与计算机程序设计艺术》 / 陈光剑
  19. 1135 mysql_mysql中一个普通ERROR 1135 (HY000)错误引发的血案
  20. CdSe/ZnTe Ⅱ型核壳量子点/核壳型功能/SiC碳化硅量子点的合成

热门文章

  1. 七牛云存储之文件上传(Android)
  2. 删除word文档中的空白页
  3. linux下如何关闭端口占用,Linux端口被占用? -- Linux查看端口使用状态、关闭端口方法...
  4. LR9.10破解方法。
  5. Linux常用命令大全 阶段性总结(二)
  6. Python RPM包制作
  7. 解决warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  8. 阿里 vs. 腾讯,谁的收购更有眼光?
  9. 对三极管特性曲线的理解
  10. 2008年9月23号,星期二,晴。博观而约取,厚积而薄发。(苏轼)