简单的自定义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 ...
随机推荐
- tornado 模板引擎
在tornado的模板引擎中,有两种方式,UImethod与UImodule 自定义方法 在模板中调用方法: tornado:与Django一样使用{{}},但是对于for循环之类,Django以{% ...
- 【爬虫】使用xpath与lxml移除特定标签
移除标签的两种方式 可以用xpath定位 for bad in html.xpath(".//table"): bad.getparent().remove(bad) 参考:htt ...
- 【LeetCode】无重复字符串最长子串
题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...
- ERROR 1050 (42S01): Table xxx already exists
今天遇到一个关于MySQL求助的问题,修改表结构时遇到"ERROR 1050 (42S01): table xxx already exits" mysql> ALTER ...
- js实现进度条
不多说,直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 原生js :removeClass和addClass
function removeClass(obj, aClass) { var re = new RegExp('\\b' + aClass + '\\b'); if (obj.className ! ...
- 个人对于 Maven 的理解
个人对于 Maven 的理解 Maven 一直都在使用, 但如果说是不是真的懂 Maven, 很难谈得上. 或许什么时候系统地学习一下, 但在那之前, 打算先记下自己目前对于 Maven 的理解, 之 ...
- Cs231n课堂内容记录-Lecture2-Part2 线性分类
Lecture 3 课程内容记录:(上)https://zhuanlan.zhihu.com/p/20918580?refer=intelligentunit (中)https://zhuanlan. ...
- Windows平台搭建-----C语言
上期我们已经进行Linux的平台搭建,今期我们就来搭建下我们最常用的.最适合初学者的一种方式,那就是搭建Windows平台开发环境,只需要两种工具即可,一个就是编辑器(编辑代码的工具),另一个就是编译 ...
- ps流提取H264并解码播放
因为需要从海康ps流中提取H264数据并进行解码播放,才有了这篇文章.因为是视频编解码领域的纯入门新手,个别理解或者方法有误,需要自行判断,不过相关方法已经测试通过,对于 像我这样的新手还是有一定的借 ...