实现正方形布局SquareLayout的几种方法

2024-05-04 12:38

本文主要是介绍实现正方形布局SquareLayout的几种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面介个方法都是复写 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
方法1

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));  // Children are just made to fill our space.  int childWidthSize = getMeasuredWidth();  int childHeightSize = getMeasuredHeight();  //高度和宽度一样  heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
}

方法2

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, widthMeasureSpec);/重写此方法后默认调用父类的onMeasure方法,分别将宽度测量空间与高度测量空间传入
super.onMeasure(widthMeasureSpec, heightMeasureSpec);/}

方法3

*/
public class SquareLayout extends FrameLayout {private int orientation = LinearLayout.HORIZONTAL;public SquareLayout(Context context) {super(context);}private void init(Context context, AttributeSet attrs, int defStyleAttr) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SquareLayout, defStyleAttr, 0);int index = typedArray.getInt(R.styleable.SquareLayout_square_orientation, -1);
//        int index = MyInputConnectionWrapper.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1);if (index >= 0) {setOrientation(index);}typedArray.recycle();}public SquareLayout(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs, 0);}public SquareLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(orientation == LinearLayout.HORIZONTAL ? widthMeasureSpec : heightMeasureSpec, orientation == LinearLayout.HORIZONTAL ? widthMeasureSpec : heightMeasureSpec);super.onMeasure(widthMeasureSpec, heightMeasureSpec);//核心就是下面这块代码块啦
//        int width = orientation == LinearLayout.HORIZONTAL ? getMeasuredWidth() : getMeasuredHeight();if (orientation == LinearLayout.HORIZONTAL) {setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);} else {setMeasuredDimension(heightMeasureSpec, heightMeasureSpec);}int width = getMeasuredWidth();int height = getMeasuredHeight();ViewGroup.LayoutParams lp = getLayoutParams();lp.height = orientation == LinearLayout.HORIZONTAL ? width : width;lp.width = orientation == LinearLayout.HORIZONTAL ? width : height;setLayoutParams(lp);}public void setOrientation(int orientation) {this.orientation = orientation;}}

我本人用第一种和第三种方法,不过都发现在某些情况下会出现毛病,第二种方法没用过

官方的方式

*/
public class SquareFrameLayout extends FrameLayout {public SquareFrameLayout(Context context) {super(context);}public SquareFrameLayout(Context context, AttributeSet attrs) {super(context, attrs);}public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public SquareFrameLayout(Context context, AttributeSet attrs,int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {final int widthSize = MeasureSpec.getSize(widthMeasureSpec);final int heightSize = MeasureSpec.getSize(heightMeasureSpec);if (widthSize == 0 && heightSize == 0) {// If there are no constraints on size, let FrameLayout measuresuper.onMeasure(widthMeasureSpec, heightMeasureSpec);// Now use the smallest of the measured dimensions for both dimensionsfinal int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());setMeasuredDimension(minSize, minSize);return;}final int size;if (widthSize == 0 || heightSize == 0) {// If one of the dimensions has no restriction on size, set both dimensions to be the// on that doessize = Math.max(widthSize, heightSize);} else {// Both dimensions have restrictions on size, set both dimensions to be the// smallest of the twosize = Math.min(widthSize, heightSize);}final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);super.onMeasure(newMeasureSpec, newMeasureSpec);}
}

这篇关于实现正方形布局SquareLayout的几种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola