简单的自定义ViewGroup
自定义ViewGroup需要重写onMeasure, onLayout等方法。下面是一个实例,4个View分别显示在四个角。
public class MyGroup extends ViewGroup{
private View viewA, viewB, viewC, viewD;
public MyGroup(Context context) {
this(context, null);
}
public MyGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
// 计算出所有的childView的宽和高
measureChildren(widthMeasureSpec, heightMeasureSpec);
int totalW1 = 0, totalH1 = 0;
int totalW2 = 0, totalH2 = 0;
for(int i=0; i<getChildCount(); ++i){
View child = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams)child.getLayoutParams();
int cw = child.getMeasuredWidth(), ch = child.getMeasuredHeight();
int lm = params.leftMargin, rm = params.rightMargin;
int tm = params.topMargin, bm = params.bottomMargin;
if(i == 0){
totalW1 += lm + cw + rm;
totalH1 += tm + ch + bm;
}
else if(i == 1){
totalW1 += lm + cw + rm;
totalH2 += tm + ch + bm;
}
else if(i == 2){
totalW2 += lm + cw + rm;
totalH1 += tm + ch + bm;
}
else if(i == 3){
totalW2 += lm + cw + rm;
totalH2 += tm + ch + bm;
}
}
int width = Math.max(totalW1, totalW2);
int height = Math.max(totalH1, totalH2);
int targetWidth = sizeWidth;
int targetHeight = sizeHeight;
if(widthMode == MeasureSpec.AT_MOST){
targetWidth = width;
}
if(heightMode == MeasureSpec.AT_MOST){
targetHeight = height;
}
setMeasuredDimension(targetWidth, targetHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for(int i=0; i<getChildCount(); ++i){
View child = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams)child.getLayoutParams();
int cw = child.getMeasuredWidth();
int ch = child.getMeasuredHeight();
int lm = params.leftMargin;
int tm = params.topMargin;
int rm = params.rightMargin;
int bm = params.bottomMargin;
if(i == 0){
child.layout(lm, tm, lm+cw, tm+ch);
}
else if(i == 1){
child.layout(getWidth()-rm-cw, tm, getWidth()-rm, tm+ch);
}
else if(i == 2){
child.layout(lm, getHeight()-bm-ch, lm+cw, getHeight()-bm);
}
else if(i == 3){
child.layout(getWidth()-rm-cw, getHeight()-bm-ch, getWidth()-rm, getHeight()-bm);
}
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// return super.generateLayoutParams(attrs);
return new MarginLayoutParams(getContext(), attrs);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
}
简单的自定义ViewGroup的更多相关文章
- 简单的例子了解自定义ViewGroup(一)
在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...
- Android ViewDragHelper完全解析 自定义ViewGroup神器
Android ViewDragHelper完全解析 自定义ViewGroup神器 转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...
- Android动画效果之自定义ViewGroup添加布局动画
前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- Android自定义ViewGroup
视图分类就两类,View和ViewGroup.ViewGroup是View的子类,ViewGroup可以包含所有的View(包括ViewGroup),View只能自我描绘,不能包含其他View. 然而 ...
- [Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析
reference to : http://www.linuxidc.com/Linux/2014-12/110165.htm 前一篇文章主要讲了自定义View为什么要重载onMeasure()方法( ...
- android 手把手教您自定义ViewGroup(一)
1.概述 在写代码之前,我必须得问几个问题: 1.ViewGroup的职责是啥? ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属 ...
- Android 自定义ViewGroup
前面几节,我们重点讨论了自定义View的三板斧,这节我们来讨论自定义ViewGroup,为什么要自定义ViewGroup,其实就是为了更好的管理View. 自定义ViewGroup无非那么几步: Ⅰ. ...
- android自定义viewgroup实现等分格子布局
先上效果图: 实现这样的效果: 一般的思路就是,直接写布局文件,用LinearLayout 嵌套多层子LinearLayout,然后根据权重layout_weight可以达到上面的效果 还有就是利用g ...
随机推荐
- View体系之属性动画
(内容省略了valueAnimator和PropertyValueHolder使用) 属性动画的使用的主要方式是AnimatorSet和ObjectAnimator配合使用.ObjectAnimato ...
- Kotlin入门(24)如何自定义视图
Android提供了丰富多彩的视图与控件,已经能够满足大部分的业务需求,然而计划赶不上变化,总是有意料之外的情况需要特殊处理.比如PagerTabStrip无法在布局文件中指定文本大小和文本颜色,只能 ...
- php post接口,登录功能
登录功能同注册功能一样,都是使用 post 方法,在执行 sql 语句时,同样要使用 "select * from 表名 where 键名 = 参数" 的查询方式,不同的是: 注册 ...
- (python)面向对象
一.面向对象概述 要了解面向对象,就需要先了解面向过程的概念,那么什么是面向过程编程呢?最具代表性的就是C语言了,所谓面向过程编程就是在做一件事的时候,需要按步骤进行,第一步干什么,第二步干什么,这种 ...
- 服务器 'xxxx' 已被定义为分发服务器。若要将该服务器重新配置为分发服务器,必须首先卸载现有的分发服务
使用AWS DMS(Database Migration Service)将SQL Server数据库同步到AWS的Data Lake上,需要在本地源数据库上配置复制,在配置分发向导最后一步时,遇到下 ...
- mysql练习----More JOIN operations
This tutorial introduces the notion of a join. The database consists of three tables movie , actor a ...
- Sql Server XML
实验数据: Create table xmldata (name NVARCHAR(20), age int, sex NVARCHAR(5) ) INSERT INTO xmldata VALUES ...
- c/c++ 友元的简单应用
友元的简单应用 1,对象 + 对象,或者,对象 + 数字,可以用类的成员函数去重载+号函数,但是,数字 + 对象就不能用类的成员函数去重载+号函数了, 因为编译器会把数字 + 对象翻译成数字.oper ...
- AI学习---回归和聚类算法
其他 资料链接:https://pan.baidu.com/s/1ofN2QFxpzC-OtmTFE2fHfw 提取码:o4c2
- ES5-ES6-ES7_数组的扩展
Array.prototype.indexOf(value) 得到值在数组中的第一个下标,如果元素不存在返回-1,可以用来判断是否包含指定的元素 var arr = [6,5,4,3,1,7,6]; ...