自定义viewgroup,这个东西可以说简单也简单,说复杂也复杂。主要是因为用到所以复习了一下,那就顺便做个笔记。

暂时只讲简单的用法

一.重要方法

(1)onMeasure 设置viewgroup的大小

(2)onLayout 设置如何摆放子View

(3)generateLayoutParams 设置LayoutParams

最重要的是前面两个方法,所以说viewgroup很简单,你只需要知道在onMeasure 和 onLayout中写什么内容就行。

注:这里说的自定义viewgroup是值直接继承ViewGroup,而不是继承各种Layout之类已封装好的ViewGroup。

1.确定自己要做的viewgroup是怎么样的

首先要确认自己要做出怎样的viewgroup,因为onMeasure和onLayout 可以说是关联很小的,他们是配合使用才能出现自己想要的效果,如果不注意细节的话很容易弄错,所以要先确定自己想做出来的viewgroup是怎样的,才开始做。

2.onMeasure

首先要记好自定义onMeasure的流程,他会先调用onMeasure再调用onLayout,而onMeasure会调用多次,这个以后讲。

(1)onMeasure做的事很简单,就是测量ViewGroup的大小,准确来说是根据子View来测量viewgroup的大小。所以在onMeasure方法里面一般会用measureChildren去测量子view的大小。

(2)onMeasure方法中一般要分两种情况去测量,viewgroup在xml中定义时,宽高是不是wrap_content

你想想,如果viewgroup固定宽高或者填充父布局的话,那实际中的宽高肯定是你定义的,但是如果是wrap_content的话,你就需要自己去设置宽高让它包裹所有子View,所以自定义viewgroup的onMeasure中会分两种情况去setMeasuredDimension宽高

2.onLayout

设置完viewgroup的宽高之后,就要去摆放子view。

(1)摆放子View的规则是,设置这个view的左上角的点在viewgroup的位置:

child.layout(left, top, right,bottom);

而根据这个坐标点和宽高,我们就能在viewgroup中正确的摆放子view

(2)需要注意的是如果子view超出了viewgroup所onMeasure(设置好大小)的部分,那部分不会显示出来。

(3)获取子view的方法View child = getChildAt(i); 得到的view一般是addview时添加view的顺序,但是还有特殊情况,这个过后再解释。

3.generateLayoutParams

设置LayoutParams,那么LayoutParams是什么东西,一般我们给viewgroup添加view都会用到LayoutParams。

翻译过来就是布局参数,通俗点说就是能获取到布局一些特定的属性,比如说布局的边距什么的。反正你正着想,在创建view时LayoutParams设置的属性,在自定义Viewgroup中都能拿到。

这个类系统有很多子类,包括如果你牛逼的话你可以依照谷歌的这种做法,可以自定义LayoutParams,所以具体情况再说。

二.demo

逼逼了这么多,还是应该拿个例子来说,比如说流式布局

流式布局可以用自定义viewgroup来实现,虽然它也可以用recyclerview来实现,但是它的性质和RelativeLayout这些布局一样,应该是一个viewgroup。

我就找了网上一个来说啊,因为我懒得写算法。

public class BerFlowLayout extends ViewGroup {

//存储所有子View

private List> mAllChildViews = new ArrayList<>();

//每一行的高度

private List mLineHeight = new ArrayList<>();

public BerFlowLayout(Context context) {

super(context);

}

public BerFlowLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

public BerFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//父控件传进来的宽度和高度以及对应的测量模式

int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

int modeWidth = MeasureSpec.getMode(widthMeasureSpec);

int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

//如果当前ViewGroup的宽高为wrap_content的情况

int width = 0;//自己测量的 宽度

int height = 0;//自己测量的高度

//记录每一行的宽度和高度

int lineWidth = 0;

int lineHeight = 0;

//获取子view的个数

int childCount = getChildCount();

for(int i = 0;i < childCount; i ++){

View child = getChildAt(i);

//测量子View的宽和高

measureChild(child, widthMeasureSpec, heightMeasureSpec);

//得到LayoutParams

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

//子View占据的宽度

int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

//子View占据的高度

int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

//换行时候

if(lineWidth + childWidth > sizeWidth){

//对比得到最大的宽度

width = Math.max(width, lineWidth);

//重置lineWidth

lineWidth = childWidth;

//记录行高

height += lineHeight;

lineHeight = childHeight;

}else{//不换行情况

//叠加行宽

lineWidth += childWidth;

//得到最大行高

lineHeight = Math.max(lineHeight, childHeight);

}

//处理最后一个子View的情况

if(i == childCount -1){

width = Math.max(width, lineWidth);

height += lineHeight;

}

}

//wrap_content

setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,

modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

mAllChildViews.clear();

mLineHeight.clear();

//获取当前ViewGroup的宽度

int width = getWidth();

int lineWidth = 0;

int lineHeight = 0;

//记录当前行的view

List lineViews = new ArrayList();

int childCount = getChildCount();

for (int i = 0; i < childCount; i++) {

View child = getChildAt(i);

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

int childWidth = child.getMeasuredWidth();

int childHeight = child.getMeasuredHeight();

//如果需要换行

if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width) {

//记录LineHeight

mLineHeight.add(lineHeight);

//记录当前行的Views

mAllChildViews.add(lineViews);

//重置行的宽高

lineWidth = 0;

lineHeight = childHeight + lp.topMargin + lp.bottomMargin;

//重置view的集合

lineViews = new ArrayList();

}

lineWidth += childWidth + lp.leftMargin + lp.rightMargin;

lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);

lineViews.add(child);

}

//处理最后一行

mLineHeight.add(lineHeight);

mAllChildViews.add(lineViews);

//设置子View的位置

int left = 0;

int top = 0;

//获取行数

int lineCount = mAllChildViews.size();

for (int i = 0; i < lineCount; i++) {

//当前行的views和高度

lineViews = mAllChildViews.get(i);

lineHeight = mLineHeight.get(i);

for (int j = 0; j < lineViews.size(); j++) {

View child = lineViews.get(j);

//判断是否显示

if (child.getVisibility() == View.GONE) {

continue;

}

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

int cLeft = left + lp.leftMargin;

int cTop = top + lp.topMargin;

int cRight = cLeft + child.getMeasuredWidth();

int cBottom = cTop + child.getMeasuredHeight();

//进行子View进行布局

child.layout(cLeft, cTop, cRight, cBottom);

left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

}

left = 0;

top += lineHeight;

}

}

/**

* 与当前ViewGroup对应的LayoutParams

*/

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

@Override

protected LayoutParams generateDefaultLayoutParams() {

return new MarginLayoutParams(LayoutParams.MATCH_PARENT,

LayoutParams.MATCH_PARENT);

}

@Override

protected LayoutParams generateLayoutParams(LayoutParams p) {

return new MarginLayoutParams(p);

}

}

1.onMeasure

先看onMeasure,看看它怎么测量整体父布局的。这里循环处理子view,先获取到子view的大小

//子View占据的宽度

int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

//子View占据的高度

int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

然后判断换不换行,如果这个子view的宽度加上前面累计起来的比父布局的宽度宽,那就加一行。

其实这里的onMeasure意图很容易看懂,它的作用就是根据子view来决定高度,所以为什么我之前说要先弄清楚你想做怎么样的效果,比如这里,我想做的效果就是流式布局的效果,那这个布局的高度肯定是根据有多少行来动态决定的吧,而这里的计算就是觉得这个高度的过程。

1.onLayout

这个他这里写得有点麻烦,应该是可以再缩短一些的。

如果累加的宽度+当前子view的宽度+间距 > 一行的宽度,则换行

if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width) {

}

换行就设置累计宽度为0: lineWidth = 0;

mAllChildViews就是它存储的第一个装view的二维数组。

然后再对每一行进行操作 for (int i = 0; i < lineCount; i++) {...},然后对left 和top 进行叠加操作。

其实我觉得这里可以在最上面的循环中就直接child.layout对子View进行布局,不用两次循环。他这里的思路是第一次大循环来获取行数并保存二维数组,第二次大循环再设置子view位置。

3.MarginLayoutParams

这里写的

/**

* 与当前ViewGroup对应的LayoutParams

*/

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

@Override

protected LayoutParams generateDefaultLayoutParams() {

return new MarginLayoutParams(LayoutParams.MATCH_PARENT,

LayoutParams.MATCH_PARENT);

}

@Override

protected LayoutParams generateLayoutParams(LayoutParams p) {

return new MarginLayoutParams(p);

}

是为了设置子view和子view间通过margin设置的间距。

4.调用

如果在xml中写子布局,可以直接用,这时设置generateLayoutParams会默认调用3个种的这个方法,你不用去关系LayoutParams。

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

但是如果是动态去添加view,你就需要自己去写MarginLayoutParams,那么可以这样写。

ViewGroup.LayoutParams lp = new ViewGroup.

LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(lp);

mlp.setMargins(10,10,10,10);

textView.setLayoutParams(mlp);

三.总结

最后做个小结吧。你可以看成自定义ViewGroup不难,它就要求你会用两个方法去测量和摆放,难的是什么呢?难的是你要怎么去写算法来完成你这个viewgroup的实现,也就是两个方法中具体的代码实现,还有就是这两个方法连起来的效果和与LayoutParams配合的效果,主要是onMeasure和onLayout的配合,要非常的注意细节,比如你在onLayout设置间距,但是在onMeasure没有去开辟这个间距所需要的空间,那就会出问题,这种写多就会清楚了。

android 自定义viewgroup 布局,Android 自定义ViewGroup(一)相关推荐

  1. Android自定义组合布局,Android 流式布局 + 自定义组合控件

    自定义组合控件 package yanjupeng.bawei.com.day09.two; import android.content.Context; import android.util.A ...

  2. android仿qq布局,Android自定义布局实现仿qq侧滑部分代码

    自定义布局实现仿qq侧滑部分android代码,供大家参考,具体内容如下 实现说明: 通过自定义布局实现: slidinglayout继承于 horizontalscrollview /** * cr ...

  3. android 自定义输入法布局,Android自定义输入法使用方法

    Android自定义输入法使用方法 时间:2017-04-21     来源:Android开发学习网 对于Android用户而言,一般都会使用第三方的输入法.可是在输入密码时(尤其是支付相关的密码) ...

  4. android 自定义输入法布局,Android InputMethodService|KeyboardView 自定义输入法和键盘 01...

    如何自定义 安卓输入法 和 键盘 1.首先有几个关键类 1.InputMethodService 2.Keyboard 3.KeyboardView 1.1 InputMethodService 看下 ...

  5. android 开发打赏布局,Android自定义View模仿虎扑直播界面的打赏按钮功能

    Android自定义View模仿虎扑直播界面的打赏按钮功能 发布时间:2020-09-28 12:15:53 来源:脚本之家 阅读:77 作者:shenhuniurou 前言 作为一个资深篮球爱好者, ...

  6. android动态居中布局,Android动态添加布局的两种方式

    释放双眼,带上耳机,听听看~! 前言 大多数时候我们布局都是用xml来布局的,但有些时候也是会用到动态布局的,尤其是在一些大项目中,动态布局更是体现的淋漓尽致. 所以今天我们就来学习一些动态加添布局的 ...

  7. android win8风格布局,Android仿Win8界面开发

    本文将要模仿Win8界面的一个设计,一个一个的方块.方法很简单.这里自己把图片改改就可以成为自己想要的界面了. 1.首先来看看自定义的MyImageView: package com.example. ...

  8. android梅花形布局,Android相对布局实现各种梅花效果

    一.效果图如下: 第一部是往drawable文件夹里放一个图片名为icon.png的图片,首先在这里说明Android只能放扩展名为png.jpg.gif的图片: 然后编写main.xml文件,代码如 ...

  9. android 动态绘制布局,Android代码和绘制曲线中按钮和绘图板的动态布局

    时间: 2019年1月11日 本文向您介绍Android代码中的按钮和绘图板的动态布局和绘制曲线,主要包括示例android 动态绘制曲线,应用技巧,基本知识和知识android 动态绘制曲线,包括A ...

  10. android动态改变布局,Android 动态添加布局的两种方式

    前言 大多数时候我们布局都是用xml来布局的,但有些时候也是会用到动态布局的,尤其是在一些大项目中,动态布局更是体现的淋漓尽致. 所以今天我们就来学习一些动态加添布局的两种方式,分别是 动态添加xml ...

最新文章

  1. 关于DataGrid等控件中的自动编号
  2. Exchange 2003 在多域环境中的部署
  3. ASP.NET ListView控件基本操作
  4. jdb java_JDB - 介绍
  5. bzoj3507: [Cqoi2014]通配符匹配
  6. Delphi中TWebBrowser中注入Js
  7. input select 值得绑定与获取
  8. 【LightOJ - 1079】Just another Robbery(概率dp,概率背包)
  9. 疲劳驾驶样本集_欧洲要求,2022年开始新车必须配备DMS(防疲劳预警)系统
  10. 美图秀秀滤镜之饱和度
  11. Centos6.6部署Redis集群
  12. 在PS中读取敏感数据
  13. python wx包_python的wxpython包
  14. 浏览器请求头 request headers转换为字典小工具
  15. 如何设置点击listview的任何位置都可以选中checkbook
  16. 视频无法播放,视频打不开怎么办?可用这款视频修复工具快速修复
  17. Android Studio集成NDK开发环境
  18. 报表分析软件有哪些呢?不急不急,给你推荐几款好用的
  19. 计算机组成原理-存储系统
  20. 【42】android Context深度剖析

热门文章

  1. API接口名称(item_search - 按关键字搜索淘宝商品)[item_search,item_get,item_search_shop等]
  2. 计算机出现假桌面怎么解决办法,Win10系统下“AppHangXProcB1”导致桌面频繁假死如何解决?...
  3. office各种格式文件对应的MIME Type/http:Content-Type
  4. Windows10彻底关闭安全中心
  5. Seo搜索引擎优化概述
  6. 树莓派4+神经计算棒二代开发-环境搭建
  7. 红米ac2100路由器刷入openwrt教程
  8. latex-符号和长度
  9. 大数据在职研究生哪个好_在职研究生大数据专业怎么样?
  10. 【快递100】 物流公司对应编码分享(截止到2021-09-19 最新数据)