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

相关文章

Golang实现基于角色的访问控制(RBAC)的项目实践

《Golang实现基于角色的访问控制(RBAC)的项目实践》基于角色的访问控制(RBAC)是一种安全机制,通过角色来管理用户权限,本文介绍了一种可落地、易扩展的GolangRBAC实现方案,具有一定... 目录一、RBAC 核心模型设计二、RBAC 核心逻辑实现RBAC 管理器定义基础 CRUD:添加用户

Python使用Spire.PDF实现为PDF添加水印

《Python使用Spire.PDF实现为PDF添加水印》在现代数字化办公环境中,PDF已成为一种广泛使用的文件格式,尤其是在需要保持文档格式时,下面我们就来看看如何使用Python为PDF文件添加水... 目录一、准备工作二、实现步骤1. 导入必要的库2. 创建 PdfDocument 对象3. 设置水印

python循环引用和解决过程

《python循环引用和解决过程》文章讨论了在Python中解决循环引用问题的几种方法,包括延迟导入、使用`importlib`、重构代码和类型提示的前向引用,这些方法可以帮助开发者避免导入循环导致的... 目录模拟循环引用解决python循环引用的方法有几种,以下是一些常见的解决android方案常见问题

python在word中插入目录和更新目录实现方式

《python在word中插入目录和更新目录实现方式》文章主要介绍了如何在Word文档中插入和更新目录,并提供了具体的代码示例,插入目录时,需要使用`TablesOfContents`对象,并设置使用... 目录1、插入目录2、更新目录总结1、插入目录需要用到对象:TablesOfContents目录的

PostgreSQ数据库实现在Windows上异地自动备份指南的详细教程

《PostgreSQ数据库实现在Windows上异地自动备份指南的详细教程》这篇文章主要为大家详细介绍了如何在Windows系统上实现PostgreSQL数据库的异地自动备份,文中的示例代码讲解详细,... 目录前期准备实现步骤步骤一:创建备份脚本步骤二:配置免密登录(可选但推荐)步骤三:设置任务计划程序步

Centos7 firewall和docker冲突问题及解决过程

《Centos7firewall和docker冲突问题及解决过程》本文描述了一个在CentOS7上使用firewalld和Docker容器的问题,当firewalld启动或重启时,会从iptable... 目录系统环境问题描述问题排查解决办法总结本文只是我对问题的记录,只能用作参考,不能China编程说明问题,请

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默