Android编程重写ViewGroup实现卡片布局的方法
作者:xurong 发布时间:2022-02-14 13:08:01
标签:Android,ViewGroup
本文实例讲述了Android编程重写ViewGroup实现卡片布局的方法。分享给大家供大家参考,具体如下:
实现效果如图:
实现思路
1. 重写onMeasure(int widthMeasureSpec, int heightMeasureSpec)设置每个子View的大小
2. 重写onLayout(boolean changed, int l, int t, int r, int b) 设置每个子View的位置
第一步:新建FlowLayout继承ViewGroup
package com.rong.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* 卡片布局
*
* @author 徐荣
*
*/
public class FlowLayout extends ViewGroup {
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 当前子View的数量
int childSize = getChildCount();
// 获取行宽
int lineWidth = getMeasuredWidth();
// 当前是第几行
int lines = 1;
// 当前累加的行宽
int nowLineWidth = 0;
for (int i = 0; i < childSize; i++) {
View view = getChildAt(i);
// 子View的宽度
int childWidth = view.getMeasuredWidth();
// 子View的高度
int childHeight = view.getMeasuredHeight();
// 如果当前的nowLineWidth+childWidth>= lineWidth 则换行
if (nowLineWidth + childWidth >= lineWidth) {
nowLineWidth = 0;
lines = lines + 1;
}
// 设置子View的位置
view.layout(nowLineWidth, childHeight * (lines - 1), nowLineWidth + childWidth, childHeight * lines);
nowLineWidth = nowLineWidth + childWidth;
// 如果nowLineWidth >= lineWidth 则换行
if (nowLineWidth >= lineWidth) {
nowLineWidth = 0;
lines = lines + 1;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 设置自己View的大小
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
// 设置每个子View的大小
view.measure(view.getMeasuredWidth(), view.getMeasuredHeight());
}
}
}
第二步:新建布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical" >
<com.rong.activity.FlowLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cup" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Double" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ear" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flower" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hotdog" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="interseting" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="joker" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="king" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mother" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lost" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="noting" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="poker" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="qustion" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ring" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="type" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vertion" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="west" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="young" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zip" />
</com.rong.activity.FlowLayout>
</RelativeLayout>
运行!
希望本文所述对大家Android程序设计有所帮助。
0
投稿
猜你喜欢
- 本文实例为大家分享了Java读取并下载网络文件的具体代码,供大家参考,具体内容如下import java.io.ByteArrayOutpu
- 本篇要点简单描述浮点数十进制转二进制精度丢失的原因。介绍几种创建BigDecimal方式的区别。整理了高精度计算的工具类。学习了阿里巴巴Ja
- Idea运行单个main方法,不编译整个工程直接上图1、选择main方法类右键->create ‘类名.main&
- 一、BitConverter 将预定义的基础类型与字节数据进行互转(Unicode)1、将值类型转成字节数组(Unicode):BitCon
- Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好。从API提供现有的图像文件
- 一.前提1.栈的内存原理图2.JVM是怎么运行方法的???1.在运行阶段的时候,classLoader类加载器会把class文件中方法对应的
- 目录背景介绍项目介绍需要知识点启动项目项目示范核心讲解核心原理功能分析分块上传秒传功能断点续传总结参考文献背景介绍 Breakpoint-
- 这篇文章主要介绍了Spring AOP AspectJ使用及配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 现在许多流行的软件中都有欢迎界面,今天就介绍一下欢迎界面的制作,由于界面涉及到页面的滑动,因此要采用ViewPager,sdk在4.0一下的
- 本文实例讲述了C#实现对数组进行随机排序类。分享给大家供大家参考。具体如下:这个一个扩充C#随机数发生器的类,可以随机生成指定范围的数字,可
- ThreadLocal 看名字 就可以看出一点头绪来,线程本地。来看一下java对他的描述:该类提供线程本地变量。这些变量与它们的正常对应变
- 读取文本文件中的内容此示例读取文本文件的内容以使用 System.IO.File 选件类的静态方法 ReadAllText 和 ReadAl
- 本文实例为大家分享了C语言实现一个扫雷小游戏的具体代码,供大家参考,具体内容如下一、全部源码//棋盘大小#define ROW 9#defi
- 整理文档,搜刮出一个Android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。详解:1.获取本地图片File文件 获取Bitma
- 本文实例讲述了spring AOP定义AfterThrowing增加处理。分享给大家供大家参考,具体如下:一 配置<?xml vers
- 前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有
- Android 控制WIFI相关操作WIFI的全称是Wireless Fidelity,又称802.11b标准,是一种高速的无线通信协议,传
- EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。ehcach
- 使用Java实现图像分割,供大家参考,具体内容如下为减少动画制作过程中的IO操作,我们可以使用连续动画来改善动画播放效率。假如我们有如下的一
- 前言:Timer是一个定时器,作为C#开发Timer控件是我们用的比较多的一个控件,它的功能很简单,但是也是值得我们去学习的一个知识点,今天