android 自定义ViewGroup实现流式布局过程

2024-06-09 01:58

本文主要是介绍android 自定义ViewGroup实现流式布局过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        谈到流式布局,有一种特性就是宽度不足,自动换行:

下面我们看看实现逻辑:

FlowLayout.java

package com.alex.flowlayout;import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;public class FlowLayout extends ViewGroup {  public FlowLayout(Context context) {  this(context, null);  }  public FlowLayout(Context context, AttributeSet attrs) {  super(context, attrs);  }  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  measureChildren(widthMeasureSpec, heightMeasureSpec);  }  @Override  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {  /** * 一般是定义为int top;一个top实际上是数组的下标 left : 指定矩形框左上角的x坐标 top: 指定矩形框左上角的y坐标 right: 指定矩形框右下角的x坐标 bottom:指定矩形框右下角的y坐标 */  int width = getWidth();  int height = getHeight();  int tw = 0;  int th = 0;  for (int i = 0; i < getChildCount(); i++) {  View child = getChildAt(i);  if (tw + child.getWidth() < width) {  } else {  tw = 0;  th += child.getMeasuredHeight();   //超过屏幕的宽度,自动换行  }  child.layout(tw, th, tw + child.getMeasuredWidth(), th + child.getMeasuredHeight());  tw += child.getMeasuredWidth();  }  }  
}  

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">  <com.alex.flowlayout.FlowLayout  android:layout_width="match_parent"  android:layout_height="match_parent">  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="将进酒" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="李白" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="李白乘舟将欲行" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="忽闻岸上踏歌声" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="桃花潭水深千尺" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="不及汪伦送我情" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="离离原上草" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="一岁一枯荣" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="野火烧不尽" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="春风吹又生" />  </com.alex.flowlayout.FlowLayout>  </LinearLayout>  

运行效果图如下:


这篇关于android 自定义ViewGroup实现流式布局过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1043938

相关文章

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码