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;
            }
        }
    }
  • 相关阅读:
    java 13-6 Char的包装类Character
    java13-5 JDK1.5以后的一个新特性和Integer的面试题
    java 13-4 Integer和String、int之间的转换,进制转换
    java 13-3 int类型的包装包Integer
    java 13-2 Arrays工具类
    java 13-1 数组高级二分查找
    java12
    kafka语句示例
    zookeeper安装
    redhat java配置
  • 原文地址:https://www.cnblogs.com/loaderman/p/10209511.html
Copyright © 2011-2022 走看看