自定义ViewGroup控件(三)-----流式布局进阶(三)

2024-08-31 23:48

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><com.example.flowlayout.FlowLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ff00ff" ><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="sfasfsfasfs"android:textColor="#43BBE7" /><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="sfas嗯啥哦 "android:textColor="#43BBE7" /><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="我真是我弄 可以的"android:textColor="#43BBE7" /><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="暗色捏你你呢你了 "android:textColor="#43BBE7" /><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="问候foe 接口就是"android:textColor="#43BBE7" /><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="暗色法俄万人 问啊"android:textColor="#43BBE7" /><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="为问啊 问问发我发"android:textColor="#43BBE7" /></com.example.flowlayout.FlowLayout></LinearLayout>

styles.xml

<resources><style name="text_flag_01"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="android:layout_margin">4dp</item><item name="android:textColor">#ffffff</item></style></resources>

flag_03.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#FFFFFF" ></solid><corners android:radius="40dp" /><paddingandroid:bottom="2dp"android:left="10dp"android:right="10dp"android:top="2dp" /></shape>

MainActivity

package com.example.flowlayout;import android.app.Activity;
import android.os.Bundle;public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}

FlowLayout

package com.example.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) {super(context);}public FlowLayout(Context context, AttributeSet attrs) {super(context, attrs);}public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/*** 父容器生成 子view 的布局LayoutParams;* 一句话道出LayoutParams的本质:LayoutParams是Layout提供给其中的Children使用的。* 如果要自定义ViewGroup支持子控件的layout_margin参数* ,则自定义的ViewGroup类必须重载generateLayoutParams()函数,* 并且在该函数中返回一个ViewGroup.MarginLayoutParams派生类对象,这样才能使用margin参数。*/@Overrideprotected LayoutParams generateLayoutParams(LayoutParams p) {return new MarginLayoutParams(p);}@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs) {return new MarginLayoutParams(getContext(), attrs);}@Overrideprotected LayoutParams generateDefaultLayoutParams() {return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int measureWidth = MeasureSpec.getSize(widthMeasureSpec);int measureHeight = MeasureSpec.getSize(heightMeasureSpec);int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);// 每行的宽度,每行的高度int lineWidth = 0;int lineHeight = 0;// 初始化容器的高度,宽地int viewGroupHeight = 0;int viewGroupWidth = 0;// 获取子view的数量int count = getChildCount();for (int i = 0; i < count; i++) {// 获取每一个子viewView child = getChildAt(i);// 测量子viewmeasureChild(child, widthMeasureSpec, heightMeasureSpec);// 如果忘记重写generateLayoutParams,则hild.getLayoutParams()将不是MarginLayoutParams的实例// 在强制转换时就会出错,此时我们把左右间距设置为0,但由于在计算布局宽高时没有加上间距值,就是计算出的宽高要比实际小,所以是onLayout时就会出错MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();// 每个子view的宽、高int childWidth = child.getMeasuredWidth() + lp.leftMargin+ lp.rightMargin;int childHeight = child.getMeasuredHeight() + lp.topMargin+ lp.bottomMargin;/*** 换行后:当在显示下一个子view的时候,之前累加 的宽度+下个子view的宽度>测量的viewGroup的宽度,就需要换行显示*/if (lineWidth + childWidth > measureWidth) {// 将容器的高度,进行累加,换行后viewGroupHeight += lineHeight;// 因为由于盛不下当前控件,而将此控件调到下一行,所以将此控件的高度和宽度初始化给lineHeight、lineWidthlineHeight = childHeight;lineWidth = childWidth;} else {// 每行的高度,需要在当前行的所有view取最大值lineHeight = Math.max(lineHeight, childHeight);// 拿第一行来说,在没有换行的情况下,每行的宽度,需要不断累加每个子view的宽度lineWidth += childWidth;}// 最后一行是不会超出width范围的,所以要单独处理if (i == count - 1) {viewGroupHeight += lineHeight;viewGroupWidth = Math.max(viewGroupWidth, lineWidth);}}// 当属性是MeasureSpec.EXACTLY时,那么它的高度就是确定的,// 只有当是wrap_content时,根据内部控件的大小来确定它的大小时,大小是不确定的,属性是AT_MOST,此时,就需要我们自己计算它的应当的大小,并设置进去setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth: viewGroupWidth,(measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight: viewGroupHeight);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int count = getChildCount();int lineWidth = 0;int lineHeight = 0;int top = 0, left = 0;for (int i = 0; i < count; i++) {View child = getChildAt(i);MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();int childWidth = child.getMeasuredWidth() + lp.leftMargin+ lp.rightMargin;int childHeight = child.getMeasuredHeight() + lp.topMargin+ lp.bottomMargin;if (childWidth + lineWidth > getMeasuredWidth()) {// 如果换行,当前控件将跑到下一行,从最左边开始,所以left就是0,而top则需要加上上一行的行高,才是这个控件的top点;top += lineHeight;left = 0;// 同样,重新初始化lineHeight和lineWidthlineHeight = childHeight;lineWidth = childWidth;} else {lineHeight = Math.max(lineHeight, childHeight);lineWidth += childWidth;}// 计算childView的left,top,right,bottomint lc = left + lp.leftMargin;int tc = top + lp.topMargin;int rc = lc + child.getMeasuredWidth();int bc = tc + child.getMeasuredHeight();child.layout(lc, tc, rc, bc);// 将left置为下一子控件的起始点left += childWidth;}}}


这篇关于自定义ViewGroup控件(三)-----流式布局进阶(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

MyBatis流式查询两种实现方式

《MyBatis流式查询两种实现方式》本文详解MyBatis流式查询,通过ResultHandler和Cursor实现边读边处理,避免内存溢出,ResultHandler逐条回调,Cursor支持迭代... 目录MyBATis 流式查询详解:ResultHandler 与 Cursor1. 什么是流式查询?

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

基于Python实现进阶版PDF合并/拆分工具

《基于Python实现进阶版PDF合并/拆分工具》在数字化时代,PDF文件已成为日常工作和学习中不可或缺的一部分,本文将详细介绍一款简单易用的PDF工具,帮助用户轻松完成PDF文件的合并与拆分操作... 目录工具概述环境准备界面说明合并PDF文件拆分PDF文件高级技巧常见问题完整源代码总结在数字化时代,PD