zoukankan      html  css  js  c++  java
  • MeasureSpec常用方法

    package com.loaderman.customviewdemo;
    
    
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class MyLinLayout extends ViewGroup {
        public MyLinLayout(Context context) {
            super(context);
        }
    
        public MyLinLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyLinLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int measureWidth = MeasureSpec.getSize(widthMeasureSpec);//获取数值
            int measureHeight = MeasureSpec.getSize(heightMeasureSpec);//获取数值
            int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);//获取模式
            int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);//获取模式
    
            int height = 0;
            int width = 0;
            int count = getChildCount();
            for (int i=0;i<count;i++) {
    
                View child = getChildAt(i);
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
                int childHeight = child.getMeasuredHeight();
                int childWidth = child.getMeasuredWidth();
    
                height += childHeight;
                width = Math.max(childWidth, width);
            }
          //设置 MeasureSpec.EXACTLY完全  MeasureSpec.UNSPECIFIED 不确定 MeasureSpec.AT_MOST 至多
            setMeasuredDimension((measureWidthMode == MeasureSpec.AT_MOST) ? measureWidth
                    : width, (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight
                    : height);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            int top = 0;
            int count = getChildCount();
            for (int i=0;i<count;i++) {
    
                View child = getChildAt(i);
    
                int childHeight = child.getMeasuredHeight();
                int childWidth = child.getMeasuredWidth();
    
                child.layout(0, top, childWidth, top + childHeight);//重新布局控件
                top += childHeight;
            }
        }
    }
  • 相关阅读:
    学习方法
    编译原理词法分析程序
    06_05_词法分析
    顺序队列的基本操作
    使用默认参数的构造函数
    基于顺序栈的进制转换
    C语言之大数相加
    输入一个年输出其天干地支纪年法的表达式
    队列的链式存储及其基本运算
    队列的顺序存储及其基本操作
  • 原文地址:https://www.cnblogs.com/loaderman/p/10209511.html
Copyright © 2011-2022 走看看