android之自定义ViewGroup和自动换行的布局的实现
viewgroup简单说就是可以装view的view.今天遇到一个问题,就是需要一个可以自动根据一行中view的宽度自动换行的布局,网上 找了下,没有相关的例子,但是找到了思路:自定义一个viewgroup,然后在onlayout文件里面自动检测view的右边缘的横坐标值,和你的 view的parent view的况度判断是否换行显示view就可以了。因为代码比较简单,就不多说了:

public class MyViewGroup extends ViewGroup {
private final static String TAG = "MyViewGroup";
3
private final static int VIEW_MARGIN=2;
5
public MyViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec);
12
for (int index = 0; index < getChildCount(); index++) {
final View child = getChildAt(index);
// measure
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
}
18
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
21
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
Log.d(TAG, "changed = "+arg0+" left = "+arg1+" top = "+arg2+" right = "+arg3+" botom = "+arg4);
final int count = getChildCount();
int row=0;// which row lay you view relative to parent
int lengthX=arg1; // right position of child relative to parent
int lengthY=arg2; // bottom position of child relative to parent
for(int i=0;i<count;i++){
30
final View child = this.getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
lengthX+=width+VIEW_MARGIN;
lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;
//if it can't drawing on a same line , skip to next line
if(lengthX>arg3){
lengthX=width+VIEW_MARGIN+arg1;
row++;
lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;
41
}
43
child.layout(lengthX-width, lengthY-height, lengthX, lengthY);
}
46
}
48
}

这里有个地方要注意,那就要明白ViewGroup的绘图流程:ViewGroup绘制包括两个步骤:1.measure 2.layout
在两个步骤中分别调用回调函数:1.onMeasure() 2.onLayout()
1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightMeasureSpec)函数存储到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被后续工作得到。
2.onLayout() 在这个函数中,ViewGroup会拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用来布局所有的childView。
3.View.MeasureSpec 与 LayoutParams 这两个类,是ViewGroup与childView协商大小用的。其中,View.MeasureSpec是ViewGroup用来部署 childView用的, LayoutParams是childView告诉ViewGroup 我需要多大的地方。
4.在View 的onMeasure的最后要调用setMeasuredDimension()这个方法存储View的大小,这个方法决定了当前View的大小。
效果图:

android之自定义ViewGroup和自动换行的布局的实现的更多相关文章
- android之自定义viewGroup仿scrollView的两种实现(滚动跟粘性)
最近一直在研究自定义控件,一般大致分为三种情况:自绘控件,组合控件,继承控件.接下来我们来看下继承控件.在此借鉴一位博主的文章,补充粘性的实现效果,并且加注自己的一些理解.大家也可以查看原文博客.an ...
- Android自定义ViewGroup,实现自动换行
学习<Android开发艺术探索>中自定义ViewGroup章节 自定义ViewGroup总结的知识点 一.自定义ViewGroup中,onMeasure理解 onMeasure(int ...
- Android 进阶自定义 ViewGroup 自定义布局
前言 在我们的实际应用中, 经常需要用到自定义控件,比如自定义圆形头像,自定义计步器等等.但有时我们不仅需要自定义控件,举个例子,FloatingActionButton 大家都很常用,所以大家也很经 ...
- Android之自定义ViewGroup
概述 在写代码之前,我必须得问几个问题: 1.ViewGroup的职责是啥? ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性, ...
- android自定义viewgroup实现等分格子布局
先上效果图: 实现这样的效果: 一般的思路就是,直接写布局文件,用LinearLayout 嵌套多层子LinearLayout,然后根据权重layout_weight可以达到上面的效果 还有就是利用g ...
- Android动画效果之自定义ViewGroup添加布局动画
前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...
- android自定义viewgroup之我也玩瀑布流
先看效果图吧, 继上一篇<android自定义viewgroup实现等分格子布局>中实现的布局效果,这里稍微有些区别,每个格子的高度不规则,就是传说的瀑布流布局,一般实现这种效果,要么用第 ...
- 自定义ViewGroup 流式布局
使用 public class MainActivity extends Activity { @Override protected void onCreate(Bundle sav ...
- Android自定义ViewGroup(四、打造自己的布局容器)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51500304 本文出自:[openXu的博客] 目录: 简单实现水平排列效果 自定义Layo ...
随机推荐
- spin_lock & mutex_lock的区别?
http://blog.csdn.net/sunnytina/article/details/7615520 为什么需要内核锁? 多核处理器下,会存在多个进程处于内核态的情况,而在内核态下,进程是 ...
- 集合对象(NSSet)
main.m #import <Foundation/Foundation.h> @interface NSSet(printInteger) -(void)printSet; @end ...
- Android 常用UI控件之TabHost(2)简单示例
1,布局 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...
- Import Items – Validation Multiple Languages Description
ð 提交标准请求创建和更新物料,因语言环境与处理次序方式等因素,造成物料中英(更多语言)描述和长描述混乱刷新. 症状: >>> Submit Standard Op ...
- centos6.5安装vbox
cd /etc/yum.repos.d wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo 下载跟CENTO ...
- c#桥接模式(bridge结构模式)
桥接模式(bridge结构模式)c#简单例子 在前面的玩家中每增加一个行为,就必须在每个玩家中都增加,通过桥接模式将行为提取出来了,减少变化 ? 1 2 3 4 5 6 7 8 9 10 11 12 ...
- OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞
漏洞名称: OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞 CNNVD编号: CNNVD-201311-117 发布时间: 2013-11-12 更新时间: 2 ...
- 宣布 Azure Backup 支持备份 Windows Server 2008
Giridhar Mosay 云 + Enterprise项目经理 AzureBackup已支持最新的 Windows Server 操作系统,例如 Windows Server 2008R2. ...
- SQL Server 2008 数据库日志文件丢失处理方法
当数据库发生这种操作故障时,可以按如下操作步骤可解决此方法,打开数据库里的Sql 查询编辑器窗口,运行以下的命令. 1.修改数据库为紧急模式 ALTER DATABASE Zhangxing SET ...
- 基于WebForm+EasyUI的业务管理系统形成之旅 -- 系统设置(Ⅰ)
上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 总体介绍>,主要介绍系统总体的界面效果和用户体验UI设计. 在MVC.MVP大行其道的今天,写WebForm该系列篇章, ...