1. package com.loaderman.customviewdemo;
  2.  
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7.  
  8. public class MyLinLayout extends ViewGroup {
  9. public MyLinLayout(Context context) {
  10. super(context);
  11. }
  12.  
  13. public MyLinLayout(Context context, AttributeSet attrs) {
  14. super(context, attrs);
  15. }
  16.  
  17. public MyLinLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  18. super(context, attrs, defStyleAttr);
  19. }
  20.  
  21. @Override
  22. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  23. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  24. int measureWidth = MeasureSpec.getSize(widthMeasureSpec);//获取数值
  25. int measureHeight = MeasureSpec.getSize(heightMeasureSpec);//获取数值
  26. int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);//获取模式
  27. int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);//获取模式
  28.  
  29. int height = 0;
  30. int width = 0;
  31. int count = getChildCount();
  32. for (int i=0;i<count;i++) {
  33.  
  34. View child = getChildAt(i);
  35. measureChild(child, widthMeasureSpec, heightMeasureSpec);
  36. int childHeight = child.getMeasuredHeight();
  37. int childWidth = child.getMeasuredWidth();
  38.  
  39. height += childHeight;
  40. width = Math.max(childWidth, width);
  41. }
  42. //设置 MeasureSpec.EXACTLY完全 MeasureSpec.UNSPECIFIED 不确定 MeasureSpec.AT_MOST 至多
  43. setMeasuredDimension((measureWidthMode == MeasureSpec.AT_MOST) ? measureWidth
  44. : width, (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight
  45. : height);
  46. }
  47.  
  48. @Override
  49. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  50. int top = 0;
  51. int count = getChildCount();
  52. for (int i=0;i<count;i++) {
  53.  
  54. View child = getChildAt(i);
  55.  
  56. int childHeight = child.getMeasuredHeight();
  57. int childWidth = child.getMeasuredWidth();
  58.  
  59. child.layout(0, top, childWidth, top + childHeight);//重新布局控件
  60. top += childHeight;
  61. }
  62. }
  63. }

MeasureSpec常用方法的更多相关文章

  1. 【Android 应用开发】 自定义 圆形进度条 组件

    转载著名出处 : http://blog.csdn.net/shulianghan/article/details/40351487 代码下载 : -- CSDN 下载地址 : http://down ...

  2. 前端开发:Javascript中的数组,常用方法解析

    前端开发:Javascript中的数组,常用方法解析 前言 Array是Javascript构成的一个重要的部分,它可以用来存储字符串.对象.函数.Number,它是非常强大的.因此深入了解Array ...

  3. Jquery元素选取、常用方法

    一:常用的选择器:(李昌辉) 基本选择器 $("#myDiv") //匹配唯一的具有此id值的元素 $("div") //匹配指定名称的所有元素 $(" ...

  4. python浅谈正则的常用方法

    python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...

  5. C# Webbrowser 常用方法及多线程调用

    设置控件的值 /// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagNa ...

  6. list,tuple,dict,set常用方法

    Python中list,tuple,dict,set常用方法 collections模块提供的其它有用扩展类型 from collections import Counter from collect ...

  7. 记录yii2-imagine几个常用方法

    记录yii2-imagine几个常用方法: //压缩 Image::thumbnail('@webroot/img/test-image.jpg', 120, 120)->save(Yii::g ...

  8. DOM常用方法总结

    DOM(Document Object Model:文档对象模型)为javascript中的一部分,它为访问和修改html文档或xml文档提供了一些编程接口,DOM以对象的形式来描述文档中的内容,以树 ...

  9. JSP内置对象及常用方法

    jsp九大内置对象及四个作用域: 何为作用域 先让我们看看效果: 大概流程是这样的,我们访问index.jsp的时候,分别对pageContext, request, session,applicat ...

随机推荐

  1. C++——虚函数表解析

     转自:https://blog.csdn.net/haoel/article/details/1948051 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型指针指 ...

  2. 常用实验报告LaTex 模板

    目录 模板1-无首页有表格头 模板2-有首页 模板1-无首页有表格头 % -*- coding: utf-8 -*- \documentclass{article} \usepackage{listi ...

  3. Django modle基础样版

    定义一个基类模版, from django.db import models class ModelBase(models.Model): """ "" ...

  4. GPT分区格式

    1. GPT定义 全局唯一标识分区表(GUID partition table, 缩写:GPT)是一个实体硬盘的分区表的结构布局的标准.它是可扩展固件接口(UEFI)标准的一部分,被用于替代BIOS系 ...

  5. Python Multiprocessing 多进程,使用多核CPU计算 并使用tqdm显示进度条

    1.背景   在python运行一些,计算复杂度比较高的函数时,服务器端单核CPU的情况比较耗时,因此需要多CPU使用多进程加快速度 2.函数要求  笔者使用的是:pathos.multiproces ...

  6. 某网站的videojs的配置及操作

    某网站的videojs的配置及操作 一.总结 一句话总结: 多参照参照别人的例子就好,省事 1.videojs如何获取用户当前视频的位置? this.currentTime() 2.回到视频开始处? ...

  7. CentOS升级Openssl至openssl-1.1.0

    1.查看原版本 wget http://www.openssl.org/source/openssl-1.1.0c.tar.gz openssl version 2.解压安装tar zxf opens ...

  8. jquery点击显示或隐藏

    点击第一个dd,给第一li添加class,点击第二个dd,给第二个li添加class,以此类推 $(function(){     $("dd > a").click(fun ...

  9. 46、[源码]-Spring容器创建-注册BeanPostProcessors

    46.[源码]-Spring容器创建-注册BeanPostProcessors 6.registerBeanPostProcessors(beanFactory);注册BeanPostProcesso ...

  10. 找到一些经验,关于使用thymeleaf时遇到的一些问题

    最近一直在使用spring boot,所以自然而然的使用了thymeleaf,但是我想说习惯了jsp之后使用thymeleaf真实觉得不顺手,在使用thymeleaf中也遇到了一些问题,在这里记录一下 ...