自定义ViewGroup(1)
源码来自 http://blog.csdn.net/lmj623565791/article/details/38339817
升级代码 http://blog.csdn.net/lmj623565791/article/details/38352503 实现FlowLayout
主要是 onMeasure 和 onLayout 的运用
package com.weidingqiang.testviewb;import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup;/*** Created by weidingqiang on 15/11/19.*/ public class BaViewGroup extends ViewGroup {public BaViewGroup(Context context) {this(context,null);}public BaViewGroup(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /*** 获得此ViewGroup上级容器为其推荐的宽和高,以及计算模式*/int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);// 计算出所有的childView的宽和高 measureChildren(widthMeasureSpec, heightMeasureSpec);/*** 记录如果是wrap_content是设置的宽和高*/int width = 0;int height = 0;int cCount = getChildCount();int cWidth = 0;int cHeight = 0;MarginLayoutParams cParams = null;// 用于计算左边两个childView的高度int lHeight = 0;// 用于计算右边两个childView的高度,最终高度取二者之间大值int rHeight = 0;// 用于计算上边两个childView的宽度int tWidth = 0;// 用于计算下面两个childiew的宽度,最终宽度取二者之间大值int bWidth = 0;/*** 根据childView计算的出的宽和高,以及设置的margin计算容器的宽和高,主要用于容器是warp_content时*/for (int i = 0; i < cCount; i++){View childView = getChildAt(i);cWidth = childView.getMeasuredWidth();cHeight = childView.getMeasuredHeight();cParams = (MarginLayoutParams) childView.getLayoutParams();// 上面两个childViewif (i == 0 || i == 1){tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;}if (i == 2 || i == 3){bWidth += cWidth + cParams.leftMargin + cParams.rightMargin;}if (i == 0 || i == 2){lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;}if (i == 1 || i == 3){rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;}}width = Math.max(tWidth, bWidth);height = Math.max(lHeight, rHeight);/*** 如果是wrap_content设置为我们计算的值* 否则:直接设置为父容器计算的值*/setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth: width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight: height);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int cCount = getChildCount();int cWidth = 0;int cHeight = 0;MarginLayoutParams cParams = null;/*** 遍历所有childView根据其宽和高,以及margin进行布局*/for (int i = 0; i < cCount; i++){View childView = getChildAt(i);cWidth = childView.getMeasuredWidth();cHeight = childView.getMeasuredHeight();cParams = (MarginLayoutParams) childView.getLayoutParams();int cl = 0, ct = 0, cr = 0, cb = 0;switch (i){case 0:cl = cParams.leftMargin;ct = cParams.topMargin;break;case 1:cl = getWidth() - cWidth - cParams.leftMargin- cParams.rightMargin;ct = cParams.topMargin;break;case 2:cl = cParams.leftMargin;ct = getHeight() - cHeight - cParams.bottomMargin;break;case 3:cl = getWidth() - cWidth - cParams.leftMargin- cParams.rightMargin;ct = getHeight() - cHeight - cParams.bottomMargin;break;}cr = cl + cWidth;cb = cHeight + ct;childView.layout(cl, ct, cr, cb);}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);}@Overridepublic ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs){return new MarginLayoutParams(getContext(), attrs);} }
布局文件
<com.weidingqiang.testviewb.BaViewGroupandroid:layout_width="300dp"android:layout_height="300dp"android:background="#ff0"><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#FF4444"android:gravity="center"android:text="0"android:textColor="#FFFFFF"android:textSize="22sp"android:textStyle="bold" /><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#00ff00"android:gravity="center"android:text="1"android:textColor="#FFFFFF"android:textSize="22sp"android:textStyle="bold" /><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#ff0000"android:gravity="center"android:text="2"android:textColor="#FFFFFF"android:textSize="22sp"android:textStyle="bold" /><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#0000ff"android:gravity="center"android:text="3"android:textColor="#FFFFFF"android:textSize="22sp"android:textStyle="bold" /></com.weidingqiang.testviewb.BaViewGroup>
转载于:https://www.cnblogs.com/weidingqiang/p/4979887.html
自定义ViewGroup(1)相关推荐
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]
http://blog.csdn.net/jj120522/article/details/8095852 示意图就不展示了,和上一节的一样,滑动菜单SlidingMenu效果如何大家都比较熟悉,在这 ...
- android 自定义图片容器,Android应用开发中自定义ViewGroup视图容器的教程
一.概述在写代码之前,我必须得问几个问题: 1.ViewGroup的职责是啥?ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性, ...
- 教你搞定Android自定义ViewGroup
ViewGroup 我们知道ViewGroup就是View的容器类,我们经常用的LinearLayout,RelativeLayout等都是ViewGroup的子类,因为ViewGroup有很多子Vi ...
- Android学习:自定义ViewGroup方法总结
毕设应用中需要添加一个滑动按钮,在网上看了几个Demo之后决定自定义ViewGroup来实现. 这里是对实现过程中自定义ViewGroup的方法总结. 关于ViewGroup,文档给出的描述是: A ...
- Android 手把手教您自定义ViewGroup
最近由于工作的变动,导致的博客的更新计划有点被打乱,希望可以尽快脉动回来~ 今天给大家带来一篇自定义ViewGroup的教程,说白了,就是教大家如何自定义ViewGroup,如果你对自定义ViewGr ...
- Android自定义ViewGroup的OnMeasure和onLayout详解
前一篇文章主要讲了自定义View为什么要重载onMeasure()方法http://blog.csdn.net/tuke_tuke/article/details/73302595 那么,自定义Vie ...
- QQ 5.0侧滑HorizontalScrollView以及自定义ViewGroup
一般侧滑的实现: 自定义的ViewGroup(menu+content) ouTouchEvent事件改变ViewGroup的LeftMargin. 大于菜单的一半显示,小于则隐藏(使用Scrol ...
- android自定义viewgroup之我也玩瀑布流
先看效果图吧, 继上一篇<android自定义viewgroup实现等分格子布局>中实现的布局效果,这里稍微有些区别,每个格子的高度不规则,就是传说的瀑布流布局,一般实现这种效果,要么用第 ...
- android 自定义viewgroup onmeasure,一篇文章搞懂Android 自定义Viewgroup的难点
本文的目的 目的在于教会大家到底如何自定义viewgroup,自定义布局和自定义测量到底如何写.很多网上随便搜搜的概念和流程图这里不再过多描述了,建议大家看本文之前,先看看基本的自定义viewgrou ...
- android 自定义flowlayout,Android 自定义ViewGroup之实现FlowLayout-标签流容器
本篇文章讲的是Android 自定义ViewGroup之实现标签流式布局-FlowLayout,开发中我们会经常需要实现类似于热门标签等自动换行的流式布局的功能,网上也有很多这样的FlowLayout ...
最新文章
- 怎么用计算机弹出惊雷,会声会影闪电效果_怎么用会声会影绘制惊雷闪电地效果_飞翔教程...
- 【资源干货】超全!我常用的70个数据分析网址
- 零基础代码学python-零基础学python之构建web应用(入门级)
- 诚毅学院全国计算机考试,集美大学2017年9月全国计算机等级考试报名时间
- intellijidea课程 intellijidea神器使用技巧 6-1 Spring的关联
- PDF课件下载!《用Python玩转数据》
- exc读入到matlab,matlab外部程序接口-excel
- 2.static关键字.rs
- Maven--资源文件resource的问题
- vivi eboot wince 烧写
- java 防止sql xxs注入,Java-JSP网站 防SQL注入,防XSS等攻击有什么好的处理办法?...
- java jvm对象_Java对象在JVM中长啥样
- easyuefi只能在基于uefi启动的_云计算学习体系-1.1-计算机硬件基础扩展知识BIOS/UEFI/MBR/GPT...
- python之禅怎么看_Python之禅
- Python 2.5.7 高阶函数
- php二分法实力,php常见的几种排序以及二分法查找
- svn图文教程-宋正河整理
- FusionChart样例
- 外挂学习之路(10)--- 穿透发包线程寻找call的通杀方法
- 网站报错类型及状态码总结
热门文章
- 【Qt】Qt下载教程
- 【Protocol Buffer】Protocol Buffer入门教程(一):简介和安装
- 【Linux系统编程】可重入函数和不可重入函数
- 职业素养和职业技能问题_中职生职业素养提升的综述
- 深度linux引导安装,全程演示Linux Deepin 12.06安装过程
- matlab验证对称三相电路,不对称三相电路中,中线的电流为()。 A.0 B. C. D....
- 每天一道LeetCode-----实现LRU置换算法
- 速来围观!leetcode java实现汇总
- MFC改变static text颜色
- 如何在VS2008中添加WM_INITDIALOG消息映射