viewgroup简单说就是可以装view的view.今天遇到一个问题,就是需要一个可以自动根据一行中view的宽度自动换行的布局,网上找了下,没有相关的例子,但是找到了思路:自定义一个viewgroup,然后在onlayout文件里面自动检测view的右边缘的横坐标值,和你的view的parent view的况度判断是否换行显示view就可以了。因为代码比较简单,就不多说了:
1 public class MyViewGroup extends ViewGroup { 2 private final static String TAG = "MyViewGroup"; 3 4 private final static int VIEW_MARGIN=2; 5 6 public MyViewGroup(Context context) { 7 super(context); 8 } 9 @Override 10 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 11 Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec); 12 13 for (int index = 0; index < getChildCount(); index++) { 14 final View child = getChildAt(index); 15 // measure16 child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 17 } 18 19 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 20 } 21 22 @Override 23 protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { 24 Log.d(TAG, "changed = "+arg0+" left = "+arg1+" top = "+arg2+" right = "+arg3+" botom = "+arg4); 25 final int count = getChildCount(); 26 int row=0;// which row lay you view relative to parent27 int lengthX=arg1; // right position of child relative to parent28 int lengthY=arg2; // bottom position of child relative to parent29 for(int i=0;i<count;i++){ 30 31 final View child = this.getChildAt(i); 32 int width = child.getMeasuredWidth(); 33 int height = child.getMeasuredHeight(); 34 lengthX+=width+VIEW_MARGIN; 35 lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2; 36 //if it can't drawing on a same line , skip to next line37 if(lengthX>arg3){ 38 lengthX=width+VIEW_MARGIN+arg1; 39 row++; 40 lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2; 41 42 } 43 44 child.layout(lengthX-width, lengthY-height, lengthX, lengthY); 45 } 46 47 } 48 49 }
这里有个地方要注意,那就要明白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提供了LayoutAnimationController类,用于为布局或者ViewGroup的子视图添加动画效果。
主要介绍了Android动画效果之自定义ViewGroup添加布局动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这是自己继承viewgroup写的布局demo,非google官方百分比布局,想学习的可以参照下
自定义ViewGroup实现流式布局demo
自定义ViewGroup实现流式布局
原文地址http://blog.csdn.net/android_cmos/article/details/52356229
详细注释并且分步骤由浅入深的讲解了自定义ViewGroup,同时完成了流式布局。
继承viewgroup, 编写自定义坐标布局,添加增删时动画操作, 拖动移除效果
http://blog.csdn.net/zxt0601/article/details/50533658 博客资源
android自定义viewgroup实现等分格子布局
Android 自定义ViewGroup实现整个Item布局竖直跑马灯效果,详细看我的博客讲解
自定义ViewGroup进一步学习,显示两个TextView到自定义控件中,对两个TextView进行布局
自定义下拉刷新,适用于多种控件,不止listview
主要给大家介绍了关于Android进阶教程之ViewGroup自定义布局的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
自定义ViewGroup卡片式页面效果、Horizontalview下标跟随宽度移动改变 主要实现的功能有: Horizontalview的item滑动字体颜色渐变(仿微信)和大小渐变。...Scrollview是重写的ViewGroup,实现的自定义卡片式布局。
主要介绍了Android编程重写ViewGroup实现卡片布局的方法,实例分析新建FlowLayout继承ViewGroup类及设置布局文件实现卡片布局效果的相关技巧,需要的朋友可以参考下
最近对自定义控件充满了兴趣,参考网上的视频写了一个流式布局的例子上传到这,和大家交流学习
自定义LayoutAnimationController,一行代码为ViewGroup设置定制顺序的布局动画!.zip,Custom LayoutAnimationController/自定义LayoutAnimationController,一行代码搞定布局动画!
可以选择传递ViewGroup布局,该布局充当动画对象的容器。 ####样本: 该图像说明了借助容器布局,恒星在地下飞行的过程,而流星和卫星则通过OverTheTopLayer在屏幕顶部飞行。 ####用法: int ...
今天看到很多app都有Title自动隐藏功能,1.这是一个Gradle工程,Gradle的版本是2.10,项目的Compile sdk Version是 23 , Build ...Android自定义ViewGroup实现Title自动隐藏功能源代码分享,Android开发者必看示例。