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

2024-08-31 23:48

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


main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.SimpleLayout.MyLinLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ff00ff"tools:context=".MainActivity" >
<!-- 在XML中添加上layout_margin参数 --><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:background="#ff0000"android:text="第一个VIEW" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background="#00ff00"android:text="第二个VIEW" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:background="#0000ff"android:text="第三个VIEW" /></com.example.SimpleLayout.MyLinLayout>

MainActivity

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

MyLinLayout

package com.example.SimpleLayout;import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;/*** /** onMeasure():测量自己的大小,自己的大小,为正式布局提供建议。(注意,只是建议,至于用不用,要看onLayout);* onLayout():使用layout()函数对所有子控件布局; onDraw():根据布局的位置绘图;* */
public class MyLinLayout extends ViewGroup {/*** 构造函数--二话不说,直接写出三个来* * @param context*/public MyLinLayout(Context context) {super(context);}public MyLinLayout(Context context, AttributeSet attrs) {super(context, attrs);}public MyLinLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/*** 如果要自定义ViewGroup支持子控件的layout_margin参数,* 则自定义的ViewGroup类必须重载generateLayoutParams* ()函数,并且在该函数中返回一个ViewGroup.MarginLayoutParams派生类对象,这样才能使用margin参数。*/@Overrideprotected LayoutParams generateLayoutParams(LayoutParams p) {return new MarginLayoutParams(p);}/*** 从指定的XML中获取对应的layout_width和layout_height值*/// 如果我们还需要margin相关的参数就只能重写generateLayoutParams()函数了:@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs) {return new MarginLayoutParams(getContext(), attrs);}/*** generateDefaultLayoutParams()函数。 直接返回对应的MarginLayoutParams()的实例*//*** 如果要使用默认的构造方法,就生成layout_width="wrap_content"、layout_height="wrap_content"* 对应的参数*//*** 为什么非要重写generateLayoutParams()函数了,就是因为默认的generateLayoutParams()* 函数只会提取layout_width* 、layout_height的值,只有MarginLayoutParams()才具有提取margin间距的功能!!!!*/@Overrideprotected LayoutParams generateDefaultLayoutParams() {return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);}/*** 此ViewGroup的宽高属性 android:layout_width="match_parent"--EXACTLY(确定)* android:layout_height="wrap_content"--AT_MOST(不确定)* * 他们是父类传递过来给当前view的一个建议值,建议值,即想把当前view的尺寸设置为宽widthMeasureSpec,* 高heightMeasureSpec* * ②、EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;* ③、AT_MOST(至多),子元素至多达到指定大小的值。*/@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);// 初始化ViewGroup宽、高int viewGroupHeight = 0;int viewGroupWidth = 0;// 获取viewGroup中的每个孩子View,进行遍历int count = getChildCount();for (int i = 0; i < count; i++) {// 依次获取每个孩子View对象View child = getChildAt(i);// 测量每个孩子View,将父类的模式传进去--点开看源码measureChild(child, widthMeasureSpec, heightMeasureSpec);// 获取MarginLayoutParams布局参数!!!!!!!!!!!!!!!!!!!!!!!/*** 由于generateLayoutParams()的返回值是LayoutParams实例,* 而MarginLayoutParams是派生自LayoutParam的* ;所以根据类的多态的特性,可以直接将此时的LayoutParams实例直接强转成MarginLayoutParams实例;* 所以下面这句在这里是不会报错的:*/MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();int childHeight = child.getMeasuredHeight() + lp.topMargin+ lp.bottomMargin;int childWidth = child.getMeasuredWidth() + lp.leftMargin+ lp.rightMargin;// ViewGroup高度递增viewGroupHeight += childHeight;// ViewGroup宽度取最大值viewGroupWidth = Math.max(childWidth, viewGroupWidth);}// ViewGroup的宽不需要测量直接"match_parent"--EXACTLY// 高是"wrap_content"--AT_MOST,需要累加得到高度/*** ②、EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;* ③、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 top = 0;int count = getChildCount();for (int i = 0; i < count; i++) {View child = getChildAt(i);// 获取MarginLayoutParams布局参数!!!!!!!!!!!!!!!!!!!!MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();int childHeight = child.getMeasuredHeight() + lp.topMargin+ lp.bottomMargin;int childWidth = child.getMeasuredWidth() + lp.leftMargin+ lp.rightMargin;child.layout(0, top, childWidth, top + childHeight);top += childHeight;}}
}


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



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

相关文章

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

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

深度解析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 (文本框