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程序设计有所帮助。


猜你喜欢
- 一、String与Date(java.util.Date)互转 1.1 String -&g
- java的Console类的使用方法及实例JDK 6中提供了java.io.Console类专用来访问基于字符的控制台设备。如果你的Java
- Android的文件存储,有I/O流的方式存储,与java一样,还有一种Android自己的SharePreferences存储方法。下面看
- DataTableDataTable 是 C# 中常用的一种数据表格类型,它类似于数据库中的表格,可以用来存储和处理数据。DataTable
- 一、回传协议接口和TCP方式实现:1.接口:import java.nio.channels.SelectionKey; import ja
- 本文实例讲述了C#使用linq语句查询数组中以特定字符开头元素的方法。分享给大家供大家参考。具体如下:下面的代码查询数组中以字母k开头的元素
- 提起ProgressBar,想必大家都比较熟悉,使用起来也是比较方便,直接在XML文件中引用,然后添加属性,运行就OK了,虽然使用Progr
- 前言反射是我们框架的灵魂,反射也是我们框架的一个底层基石,没有反射也就没有框架,如果我们学好了反射,对我们阅读框架底层是有很大班助的——阿俊
- 前言说实话当第一次看到这个需求的时候,第一反应就是Canvas只有drawLine方法,并没有drawDashLine方法啊!这咋整啊,难道
- 介绍Java桥梁模式(也称桥接模式)(Bridge Pattern)是一种设计模式,它将抽象和实现分离,使它们可以独立地变化.它通过一个大类
- TimeSpan结构:表示一个时间间隔。它含有以下四个构造函数:TimeSpan(Int64)将 TimeSpan结构的新实例初始
- 最近为公司做的一个Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的时候我发现GridView和L
- 日期格式处理在控制器中使用对象接收数据前端:<form action="test/add" method=&quo
- 前言CompletableFuture实现了CompletionStage接口和Future接口,前者是对后者的一个扩展,增加了异步回调、流
- 一、概念HttpClientAndroid 6中移除(API数量多扩展困难)。HttpURLConnection目前官方集成的。OKHttp
- 前言《JAVA打砖块》游戏是自制的游戏。玩家操作一根萤幕上水平的“棒子”,让一颗不断弹来弹去的&am
- 本文实例为大家分享了WPF实现平面三角形3D运动效果的具体代码,供大家参考,具体内容如下实现效果如下:思路:封装三角形三个顶点和路径的三角形
- 本文实例为大家分享了Android端实现文件上传的具体代码,供大家参考,具体内容如下1)、新建一个Android项目命名为androidUp
- 一、前言问题阐述:在某一场景下,我们的代码在 Service 实现相同,但却在 Controller 层访问时却希望不同的前缀可以访问。如下
- 1、概述首先和大家一起回顾一下Java 消息服务,在我之前的博客《Java消息队列-JMS概述》中,我为大家分析了:1.消息服务:一个中间件