深入理解MeasureSpec

一、MeasureSpec簡介

MeasureSpec是Android View系統中用於描述View在MeasureSpec上的要求,定義為一個32位的int類型變量。

32位的MeasureSpec中的高2位表示MeasureSpec的模式(Mode),剩下的30位表示MeasureSpec的大小(Size)。

二、MeasureSpec的三種模式

MeasureSpec的三種模式分別是UNSPECIFIED、EXACTLY和AT_MOST。

1、UNSPECIFIED模式(即為0)表示此View對尺寸沒有任何參考意義,父View可以按照子View實際尺寸分配給它任意的空間。對於UNSPECIFIED模式的子View,雖然可以設置任意的寬高,但是其實意義並不是很大,對大部分View沒有意義。

2、EXACTLY模式(即為MeasureSpec.EXACTLY,值為1073741824)表示View的確切大小已知,此時MeasureSpec中的size表示View的大小。對於EXACTLY模式的子View,想要佔據特定的大小,需要確切的設定尺寸,否則將會導致View的不確定位置。

3、AT_MOST模式(即為MeasureSpec.AT_MOST,值為-2147483648)表示此View的尺寸是最大可獲得的尺寸。對於AT_MOST模式的View,應該盡量適應於制約因素,盡量使得其不大於Specified Size的尺寸,否則就是不確定的,會影響其他控件的布局。

三、MeasureSpec的計算規則

MeasureSpec是通過父View的MeasureSpec和子View的LayoutParams來計算得到的。MeasureSpec的計算規則包括以下步驟:

1、獲取父View的MeasureSpec以及父View的padding值(因為padding的值也是父View尺寸的一部分,所以需要將padding的值算入到父View的尺寸中)。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding後的父View寬高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;}

2、通過MeasureSpec.makeMeasureSpec方法,將父View的MeasureSpec和LayoutParams中的尺寸相結合,來計算子View的MeasureSpec。

public static int makeMeasureSpec(int size, int mode) {    if (mode == UNSPECIFIED) {        return size;    } else {        return size + mode;    }}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding後的父View寬高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;    // 通過makeMeasureSpec方法,計算子View的寬度和高度MeasureSpec    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST);}

3、通過measureChildWithMargins或measureChildren方法,測量出子View實際的寬度和高度。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding後的父View寬高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;    // 通過makeMeasureSpec方法,計算子View的寬度和高度MeasureSpec    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST);    // 測量子View的寬高    measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);}

四、MeasureSpec的常見使用場景

MeasureSpec的常見使用場景包括:TableView的單元格高度、ListView/RecyclerView的Item高度、自定義View的測量等。

1、TableView的單元格高度

public void setCellWidthAndHeight(int width, int height) {    LayoutParams lp = new LayoutParams(width, height);    if (mTableLayout != null) {        TableRow tableRow = mTableLayout.findViewById(ROW_ID + mRowIndex);        View childView = tableRow.findViewById(COLUMN_ID + mColumnIndex);        if (childView != null) {            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);        }    }}

2、ListView/RecyclerView的Item高度

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);    int parentWidth = parent.getMeasuredWidth();    int itemHeight = parentWidth / 2;    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY);    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);    itemView.measure(childWidthMeasureSpec, childHeightMeasureSpec);    return new MyViewHolder(itemView);}

3、自定義View的測量

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding後的父View寬高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;    // 計算子View的寬高MeasureSpec    int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, LayoutParams.WRAP_CONTENT);    int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, LayoutParams.WRAP_CONTENT);    // 測量子View的寬高    measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248930.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 13:30
下一篇 2024-12-12 13:30

相關推薦

  • 深入解析Vue3 defineExpose

    Vue 3在開發過程中引入了新的API `defineExpose`。在以前的版本中,我們經常使用 `$attrs` 和` $listeners` 實現父組件與子組件之間的通信,但…

    編程 2025-04-25
  • 深入理解byte轉int

    一、位元組與比特 在討論byte轉int之前,我們需要了解位元組和比特的概念。位元組是計算機存儲單位的一種,通常表示8個比特(bit),即1位元組=8比特。比特是計算機中最小的數據單位,是…

    編程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什麼是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一個內置小部件,它可以監測數據流(Stream)中數據的變…

    編程 2025-04-25
  • 深入探討OpenCV版本

    OpenCV是一個用於計算機視覺應用程序的開源庫。它是由英特爾公司創建的,現已由Willow Garage管理。OpenCV旨在提供一個易於使用的計算機視覺和機器學習基礎架構,以實…

    編程 2025-04-25
  • 深入了解scala-maven-plugin

    一、簡介 Scala-maven-plugin 是一個創造和管理 Scala 項目的maven插件,它可以自動生成基本項目結構、依賴配置、Scala文件等。使用它可以使我們專註於代…

    編程 2025-04-25
  • 深入了解LaTeX的腳註(latexfootnote)

    一、基本介紹 LaTeX作為一種排版軟件,具有各種各樣的功能,其中腳註(footnote)是一個十分重要的功能之一。在LaTeX中,腳註是用命令latexfootnote來實現的。…

    編程 2025-04-25
  • 深入剖析MapStruct未生成實現類問題

    一、MapStruct簡介 MapStruct是一個Java bean映射器,它通過註解和代碼生成來在Java bean之間轉換成本類代碼,實現類型安全,簡單而不失靈活。 作為一個…

    編程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一個程序就是一個模塊,而一個模塊可以引入另一個模塊,這樣就形成了包。包就是有多個模塊組成的一個大模塊,也可以看做是一個文件夾。包可以有效地組織代碼和數據…

    編程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r為前綴的字符串。r字符串中的反斜杠(\)不會被轉義,而是被當作普通字符處理,這使得r字符串可以非常方便…

    編程 2025-04-25
  • 深入探討馮諾依曼原理

    一、原理概述 馮諾依曼原理,又稱「存儲程序控制原理」,是指計算機的程序和數據都存儲在同一個存儲器中,並且通過一個統一的總線來傳輸數據。這個原理的提出,是計算機科學發展中的重大進展,…

    編程 2025-04-25

發表回復

登錄後才能評論