测量规格说明书 MeasureSpec

View.getLayoutParams()获取的是父容器的ViewGroup.LayoutParams的类型。

因为对于普通View, 其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同确定的。

我们给View设置LayoutParams, 在测量的时候, 系统会将View的LayoutParams加上父容器的约束转换成对应的MeasureSpec, 然后再根据这个MeasureSpec来确定View测量后的宽/高。

public static class View.MeasureSpec

    public static class MeasureSpec {
        private static final int MODE_SHIFT = 30;
        private static final int MODE_MASK  = 0x3 << MODE_SHIFT;

        /** @hide */
        @IntDef({UNSPECIFIED, EXACTLY, AT_MOST})
        @Retention(RetentionPolicy.SOURCE)
        public @interface MeasureSpecMode {}

        /**
         * Measure specification mode: The parent has not imposed any constraint
         * on the child. It can be whatever size it wants.
         */
        public static final int UNSPECIFIED = 0 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has determined an exact size
         * for the child. The child is going to be given those bounds regardless
         * of how big it wants to be.
         */
        public static final int EXACTLY     = 1 << MODE_SHIFT;

        /**
         * Measure specification mode: The child can be as large as it wants up
         * to the specified size.
         */
        public static final int AT_MOST     = 2 << MODE_SHIFT;

        /**
         * Creates a measure specification based on the supplied size and mode.
         *
         * The mode must always be one of the following:
         * <ul>
         *  <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
         *  <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
         *  <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
         * </ul>
         *
         * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec‘s
         * implementation was such that the order of arguments did not matter
         * and overflow in either value could impact the resulting MeasureSpec.
         * {@link android.widget.RelativeLayout} was affected by this bug.
         * Apps targeting API levels greater than 17 will get the fixed, more strict
         * behavior.</p>
         *
         * @param size the size of the measure specification
         * @param mode the mode of the measure specification
         * @return the measure specification based on size and mode
         */
        public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
                                          @MeasureSpecMode int mode) {
            if (sUseBrokenMakeMeasureSpec) {
                return size + mode;
            } else {
                return (size & ~MODE_MASK) | (mode & MODE_MASK);
            }
        }

        /**
         * Like {@link #makeMeasureSpec(int, int)}, but any spec with a mode of UNSPECIFIED
         * will automatically get a size of 0. Older apps expect this.
         *
         * @hide internal use only for compatibility with system widgets and older apps
         */
        public static int makeSafeMeasureSpec(int size, int mode) {
            if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
                return 0;
            }
            return makeMeasureSpec(size, mode);
        }

        /**
         * Extracts the mode from the supplied measure specification.
         *
         * @param measureSpec the measure specification to extract the mode from
         * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
         *         {@link android.view.View.MeasureSpec#AT_MOST} or
         *         {@link android.view.View.MeasureSpec#EXACTLY}
         */
        @MeasureSpecMode
        public static int getMode(int measureSpec) {
            //noinspection ResourceType
            return (measureSpec & MODE_MASK);
        }

        /**
         * Extracts the size from the supplied measure specification.
         *
         * @param measureSpec the measure specification to extract the size from
         * @return the size in pixels defined in the supplied measure specification
         */
        public static int getSize(int measureSpec) {
            return (measureSpec & ~MODE_MASK);
        }

        static int adjust(int measureSpec, int delta) {
            final int mode = getMode(measureSpec);
            int size = getSize(measureSpec);
            if (mode == UNSPECIFIED) {
                // No need to adjust size for UNSPECIFIED mode.
                return makeMeasureSpec(size, UNSPECIFIED);
            }
            size += delta;
            if (size < 0) {
                Log.e(VIEW_LOG_TAG, "MeasureSpec.adjust: new size would be negative! (" + size +
                        ") spec: " + toString(measureSpec) + " delta: " + delta);
                size = 0;
            }
            return makeMeasureSpec(size, mode);
        }

        /**
         * Returns a String representation of the specified measure
         * specification.
         *
         * @param measureSpec the measure specification to convert to a String
         * @return a String with the following format: "MeasureSpec: MODE SIZE"
         */
        public static String toString(int measureSpec) {
            int mode = getMode(measureSpec);
            int size = getSize(measureSpec);

            StringBuilder sb = new StringBuilder("MeasureSpec: ");

            if (mode == UNSPECIFIED)
                sb.append("UNSPECIFIED ");
            else if (mode == EXACTLY)
                sb.append("EXACTLY ");
            else if (mode == AT_MOST)
                sb.append("AT_MOST ");
            else
                sb.append(mode).append(" ");

            sb.append(size);
            return sb.toString();
        }
    }
时间: 2024-08-01 15:43:42

测量规格说明书 MeasureSpec的相关文章

软件规格说明书

设计一个软件,要先有一个软件需求规格说明书,这是开发软件的方向,而程序员是要根椐需求规格说明书去开发软件.其具体的作用是: 便于用户.开发人员进行理解和交流:反映出用户问题的结构,可以作为软件开发工作的基础和依据:作为确认测试和验收的依据. 而具体规格说明书涉及的方面有: 1.概述,编写<需求规格说明书>的目的.软件设计总体要求:功能.性能要求.数据结构和采集要求.重要的接口要求还有软件确认测试的依据.还有,编写依据一般可以写依据什么软件的方案书,策划书等.还有一些术语和缩略词的解释.2.软件

第五次作业——团队项目——需求规格说明书

队长:031302628  叶志宇 组员:031302340 庞光莉.031302601 蔡潇.031302528 张建平 一.随笔描述:(1)计划安排:    在组队开始的第一周内,我们首先讨论下该做什么项目,最后决定和其他组一起做报课系统这个项目,写出初步的规格需求说明书,同时进行代码的学习,在此之前已经将平台搭建好了.    第二周开始进行编程,每个人实现自己负责部分的简单功能,做出规格说明书终极版,UI设计. 第三周进行UI设计改进+架构设计+测试计划,并且各个组员能够实现更高级的功能.

C#规格说明书

规格说明书 Job Market Problem Statement Version 1.0 Revision History Date Issue Description Author 23/May/2015 V1.0 Initial creation. ChenHao Problem Statement 一款求职类应用,有以下几种功能: 功能一:招聘和实习信息浏览,包括时间,标题和具体内容等 功能二:发布招聘和实习信息 功能三:填写简历 Job Market Glossary Version

规格说明书

Account Book Problem Statement Version 1.0 Revision History Date Issue Description Author 23/May/2015 4.2 Initial creation. Extracted the Problem Statement from the Vision document for purposes of scoping. Liu Jingcheng Account Book Problem Statement

(第十一周)规格说明书练习作业——吉林市一日游

假设全体同学及教师去吉林省吉林市1日游,请为这次活动给出规格说明书. 编写:杜月 用户:2016级计算机技术全体同学 日期:2016/11/23 1.引言 1.1 编写目的及背景 1.2 预期的读者和阅读建议 2.计划概述 2.1 出行目的 2.2用户分析 2.3人员分配 2.4 假定和约束 3.计划详述 3.1出行工具选择 3.2目标景点选择 3.3进餐方式选择 3.4前期准备 4.总结   1.引言   1.1 编写目的及背景 编写目的是为了详细介绍计算机技术全体28名同学以及杨贵福老师去吉

团队项目——需求规格说明书

Deadline:2016.10.22  8:00am   发表一篇随笔+课堂现场评审 随笔描述: 1)描述为撰写 需求规格说明书的 工作流程.组员分工.组员工作量比例: 2)提供 <需求规格说明书>的Git链接. 要求: 1.参考<软件需求规格说明书>国标规范文本,撰写对应项目的软件需求规格说明书. 2. 除形式上满足规范文本要求外,整体内容必须围绕项目实质展开,对所要开发的项目确保尽力做到清晰完整准确. 3.采用分层形式描述,随着"层"的深入,描述的内容细节

RUC自习助手_需求分析规格说明书

文档编号:2016051603 版本信息:v3.0         RUC自习助手 需求分析规格说明书 开发小组:找不到地方上自习组 成员:王丹丹.赵安.吴婧.杨轹丹.孟启飞.彭宇清 版本号 编写(修改)人 修改描述 修改时间 V1.0 吴婧 编写初稿 2016-3-14 V2.0 吴婧 对之前版本进行修改 2016-3-28 V3.0 吴婧 对之前版本进行修改 2016-5-16 目录 I.  引言................................................

BugPhobia进阶篇章:功能规格说明书

0x01 :特别鸣谢 首先特别鸣谢<构建之法>中并没有给出固定化格式的功能规格说明书的样例,因此在此次的说明书中将尽可能用生动形象的例子展示软件交互阐释 因此受到它本身的启发,此次团队功能规格说明书尽量用活生生的例子讲述用户和软件交互的场景,并且力求语言的简洁和直白 最后,再次鸣谢bugphobia团队本身的创造力,最终没有局限在模板中,而是能在讨论中共同挖掘出非常欢酷的想法 0x02 :前置条件阐释 0x0200 :定义 <摘要>依据康德理性批判“澄清前提,划清界限”的指导思想,

项目管理-范围管理-项目需求规格说明书

项目在客户的眼中,需求经常是含糊不清的,他们也经常道不清述不明,客户内部也常常众口不一.客户没有责任把需求整理好来告诉你,就算告诉你的也不一定就是他们最终想要的,所以作为项目经理,“范围管理”是非常重要的活动.这里主要讲述一下我作为项目经理的职能时,在项目范围管理中,进行的收集需求.定义范围.创建WBS.确认范围.控制范围的过程. 项目管理-时间管理-甘特图(http://www.cnblogs.com/wgp13x/p/4385475.html)是我的项目管理专辑的第一篇,他们都很重要. 一.