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

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推