自定义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

相关文章

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

WinForms中主要控件的详细使用教程

《WinForms中主要控件的详细使用教程》WinForms(WindowsForms)是Microsoft提供的用于构建Windows桌面应用程序的框架,它提供了丰富的控件集合,可以满足各种UI设计... 目录一、基础控件1. Button (按钮)2. Label (标签)3. TextBox (文本框

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2