• 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;
            }
        }
    }
  • 相关阅读:
    flask框架-wtforms
    flask框架-蓝图
    flask框架-请求扩展
    flask框架-中间件
    flask框架-闪现
    flask框架-session
    flask框架-请求和响应
    flask框架-模板语言
    flask框架-路由
    flask框架-配置文件
  • 原文地址:https://www.cnblogs.com/loaderman/p/10209511.html
Copyright © 2020-2023  润新知