当前位置: 代码迷 >> 综合 >> MeasureSpec
  详细解决方案

MeasureSpec

热度:95   发布时间:2023-12-16 06:18:14.0

MeasureSpec

public static class View.MeasureSpec extends Object
类概述:
A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement for either the width or the height. A MeasureSpec is comprised of a size and a mode. There are three possible modes:
//一个MeasureSpec对象封装了从父布局传给子布局的布局要求。每一个MeasureSpec代表了一个宽度或者高度的要求。一个MeasureSpec由一个尺寸和一个模式组成。有三种模式:
UNSPECIFIED//未指明的//常量: Constant Value:  0 (0x00000000)
The parent has not imposed any constraint on the child. It can be whatever size it wants.//未指明的.
//父布局没有在子布局上强加任何约束。它可以是任何它想要的.
EXACTLY//恰好地,精确地//常量: Constant Value:  1073741824 (0x40000000)
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.
//父布局已经为子布局决定了一个确切的尺寸.不论这个子布局想要多大,它都将被赋予这些边界。
AT_MOST//常量: Constant Value:  -2147483648 (0x80000000)
The child can be as large as it wants up to the specified size.//子布局可以和指定的尺寸一样大。                                                                                                 MeasureSpecs are implemented as ints to reduce object allocation. This class is provided to pack and unpack the <size, mode> tuple into the int.
//Measure以整数执行以减少对象的配置。这个类被提供用来把int打包和解包进入元组<size,mode>.
构造方法:
public View.MeasureSpec ()
方法:
(1)public static int getMode(int measureSpec)
Extracts the mode from the supplied measure specification.
//从提供的测量说明里面提取模式。
Parameters
measureSpec the measure specification to extract the mode from
Returns
  • UNSPECIFIEDAT_MOST or EXACTLY

(2)public static int getSize (int measureSpec)
Added in  API level 1

Extracts the size from the supplied measure specification.

//从提供的测量说明里面提取尺寸(像素表示)。

Parameters
measureSpec the measure specification to extract the size from
Returns
  • the size in pixels defined in the supplied measure specification.
(3)public static int makeMeasureSpec (int size, int mode)
Creates a measure specification based on the supplied size and mode. The mode must always be one of the following:
//根据提供的尺寸和模式建立一个测量的说明。模式必须是下面中的一个:
1)UNSPECIFIED
2)AT_MOST
3)EXACTLY
Note: 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. RelativeLayout was affected by this bug. Apps targeting API levels greater than 17 will get the fixed, more strict behavior.
//在低于或者等于api 17的情况下,makeMeasureSpec的实现是,参数的顺序不影响,但是值得溢出会影响MeasureSpec的结果.RelativeLayout会收到这个bug的影响.应用的API的等级大于17的将得到固定,更严格的行为。
Parameters
size the size of the measure specification
mode the mode of the measure specification
Returns
  • the measure specification based on size and mode
(4)public static String toString (int measureSpec)

Returns a String representation of the specified measure specification.

//返回一个字符串,它代表指定的测量说明。

Parameters
measureSpec the measure specification to convert to a String
Returns
  • a String with the following format: "MeasureSpec: MODE SIZE"