Android View的 getHeight 和 getMeasuredHeight 的區別

前言

先簡單復習一下Android View 的 繪制順序:

1、onMeasure(測量),先根據構造器傳進來的LayoutParams(布局參數),測量view寬高。

2、onLayout(布局),再根據測量出來的寬高參數,進行布局

3、onDraw(繪制),最后繪制出View。

ps:案例中用到了dataBinding

1、使用LayoutParams改變View高度

效果:getHeight 和 getMeasuredHeight 的值是一樣的,沒有區別;

getWidth 和 getMeasuredWidth 也是同理。

        bind.btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) bind.textBox.getLayoutParams();             params.height += 10;bind.textBox.setLayoutParams(params);Log.d("TAG", "更新后 getHeight:" + bind.textBox.getHeight() + " --- getMeasuredHeight:" + bind.textBox.getMeasuredHeight());}});

2、使用layout改變View高度

效果:getHeight 的值在變化,而 getMeasuredHeight 的值沒有變化,還是初始值。

getWidth 和 getMeasuredWidth 也是同理。

	bind.btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {bind.textBox.layout(bind.textBox.getLeft(),bind.textBox.getTop(),bind.textBox.getRight(),bind.textBox.getBottom() + 10);Log.d("TAG","更新后 getHeight:" + bind.textBox.getHeight() + " --- getMeasuredHeight:" + bind.textBox.getMeasuredHeight());}});

3、區別

通過以上方式可以看出,使用layout可以改變View 寬或高 度,但并不會更新View原始測量值,即使使用requestLayout()也不行。

源碼:因為它是final類型,不可重寫。

4、兩種獲取寬高值的應用場景

1、View布局完成(就是View執行完onLayout),使用 getWidth / getHeight,這是View源碼。

    /*** Return the width of your view.** @return The width of your view, in pixels.*/@ViewDebug.ExportedProperty(category = "layout")public final int getWidth() {return mRight - mLeft;}/*** Return the height of your view.** @return The height of your view, in pixels.*/@ViewDebug.ExportedProperty(category = "layout")public final int getHeight() {return mBottom - mTop;}/*** Right position of this view relative to its parent.** @return The right edge of this view, in pixels.*/@ViewDebug.CapturedViewPropertypublic final int getRight() {return mRight;}/*** Left position of this view relative to its parent.** @return The left edge of this view, in pixels.*/@ViewDebug.CapturedViewPropertypublic final int getLeft() {return mLeft;}/*** Bottom position of this view relative to its parent.** @return The bottom of this view, in pixels.*/@ViewDebug.CapturedViewPropertypublic final int getBottom() {return mBottom;}/*** Top position of this view relative to its parent.** @return The top of this view, in pixels.*/@ViewDebug.CapturedViewPropertypublic final int getTop() {return mTop;}

1.1、自定義View 運行結果

    @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);Log.d("TAG", "onMeasure --- getLeft:" + getLeft()); // 0Log.d("TAG", "onMeasure --- getTop:" + getTop()); // 0Log.d("TAG", "onMeasure --- getRight:" + getRight()); // 0Log.d("TAG", "onMeasure --- getBottom:" + getBottom()); // 0Log.d("TAG", "onMeasure --- getWidth:" + getWidth()); // 0Log.d("TAG", "onMeasure --- getHeight:" + getHeight()); // 0}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);Log.d("TAG", "onMeasure --- getLeft:" + getLeft()); // 503Log.d("TAG", "onMeasure --- getTop:" + getTop()); // 871Log.d("TAG", "onMeasure --- getRight:" + getRight()); // 577Log.d("TAG", "onMeasure --- getBottom:" + getBottom()); // 923Log.d("TAG", "onMeasure --- getWidth:" + getWidth()); // 74    = 577 - 503Log.d("TAG", "onMeasure --- getHeight:" + getHeight()); // 52  = 923 - 871}

2、反之View沒有布局完,使用 getMeasuredWidth / getMeasuredHeight,比如在onCreate中使用可以獲取 寬高值,如果在此 使用 getWidth / getHeight 返回會是0,因為此時還沒有布局完成。

注意:布局未完成前使用 getMeasuredWidth / getMeasuredHeight,要先主動通知系統測量,才會有值,如果布局已經完成,那就直接用,不需要這一步;

通知系統測量方法:measure(0,0),直接都寫0就好,系統會返回正確的值。

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main, null));setContentView(bind.getRoot());// 主動通知系統去測量bind.textBox的高度// 注意:在onCreate中,此時的布局還未完成,只有執行了這句代碼,bind.textBox.getMeasuredHeight() 才會有值bind.textBox.measure(0,0);bind.showHeight.setText("getHeight:" + bind.textBox.getHeight() + " --- getMeasuredHeight:" + bind.textBox.getMeasuredHeight());    }

5、使用layout改變View寬高,會引發的問題

前言提到,onLayout(布局)是根據測量出來的寬高參數,進行布局的,雖然在視覺上改變了寬高,但測量的寬高值還是原始值,沒有改變;

而在Android布局體系中,父View負責刷新、布局顯示子View,當子View需要刷新時,則是通知父View來完成,就是循環遍歷調用子View的?measure / layout / draw 方法。

由此得出,當布局中某個子View布局發生改變,這個父View就開始循環遍歷調用子View的layout,通過布爾值changed判斷當前子View是否需要重新布局,changed為true表示當前View的大小或位置改變了 。

這時就會發現 之前通過layout改變寬高的View,會被還原因為onLayout(布局)是根據測量出來的寬高參數,進行布局的,重要的話說三遍。

案例:

我新加了一個TextView,將值顯示在屏幕上。

        <TextViewandroid:id="@+id/show_height"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

View沒有發生改變?不,它改變了,但給TextView賦值后,TextView的寬高發生改變,通知父View刷新,父View開始循環遍歷子View的layout方法,導致 通過layout改變寬高的View,又根據 原始測量值,重新布局還原了,由于執行的太快,所以視覺上看不到View這個過程。

日志:

20:29:39.933 ?D ?MTextView --- onLayout --- bottom:1127 --- changed:true
20:29:39.935 ?D ?更新TextView,觸發MTextView的 onLayout方法
20:29:39.973 ?D ?MTextView --- onLayout --- bottom:1117 --- changed:true

6、案例文件

MTextView.java

public class MTextView extends androidx.appcompat.widget.AppCompatTextView {public MTextView(@NonNull Context context) {super(context);}public MTextView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int heightPxValue = MeasureSpec.getSize(heightMeasureSpec);Log.d("TAG", "MTextView --- onMeasure --- heightPxValue:" + heightPxValue);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);Log.d("TAG", "MTextView --- onLayout --- bottom:" + bottom + " --- changed:" + changed);}

app_activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><data></data><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"tools:context=".ui.activity.AppMainActivity"><TextViewandroid:id="@+id/show_height"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="update"android:textAllCaps="false" /><com.example.xxx.ui.view.MTextViewandroid:id="@+id/text_box"style="@style/Font_303133_15_bold"android:layout_width="200dp"android:layout_height="100dp"android:background="@color/color_66000000"android:gravity="center"android:text="hello world" /></LinearLayout></layout>

AppMainActivity.java

public class AppMainActivity extends AppCompatActivity {private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main, null));setContentView(bind.getRoot());// bind.btn.setOnClickListener(new View.OnClickListener() {//    @Override//    public void onClick(View v) {//         LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) bind.textBox.getLayoutParams();             //         params.height += 10;//         bind.textBox.setLayoutParams(params);//         Log.d("TAG", "更新后 getHeight:" + bind.textBox.getHeight() + " --- getMeasuredHeight:" + bind.textBox.getMeasuredHeight());//    }// });// bind.btn.setOnClickListener(new View.OnClickListener() {//    @Override//    public void onClick(View v) {//        bind.textBox.layout(//                bind.textBox.getLeft(),//                bind.textBox.getTop(),//                bind.textBox.getRight(),//                bind.textBox.getBottom() + 10//        );//        Log.d("TAG","更新后 getHeight:" + bind.textBox.getHeight() + " --- getMeasuredHeight:" + bind.textBox.getMeasuredHeight());//    }// });bind.btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {bind.textBox.layout(bind.textBox.getLeft(),bind.textBox.getTop(),bind.textBox.getRight(),bind.textBox.getBottom() + 10);bind.showHeight.setText("getHeight:" + bind.textBox.getHeight() + " --- getMeasuredHeight:" + bind.textBox.getMeasuredHeight());Log.d("TAG","更新TextView,觸發MTextView的 onLayout方法");}});}}

總結

在View沒有布局完成前,想要獲取 寬高,使用 getMeasuredWidth / getMeasuredHeight,記得先通知系統測量;

反之只要顯示在屏幕上,getWidth / getHeight 就能拿到值,還是時時數據。

補充一下,如果在xml中給View設置了visibility="gone"注意是xml,getWidth / getHeight 也拿不到值,如果是?visibility="invisible",不受影響。

再如果?在xml中給View設置了visibility="gone",在代碼中設置成setVisibility(View.VISIBLE)第一次拿不到值,因為還沒有layout完成,之后就可以拿到了,后面再給它設置成setVisibility(View.GONE),也不會受影響,因為已經布局過了。代碼在這,我都試過了,核心就是看View有沒有onLayout完成。

Activity

    @Overridepublic void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);Log.d("TAG", "getHeight:" + bind.btn.getHeight()); // 0new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Log.d("TAG", "getHeight:" + bind.btn.getHeight()); // 0bind.btn.setVisibility(View.VISIBLE); // 設置顯示Log.d("TAG", "getHeight:" + bind.btn.getHeight()); // 0}}, 2000);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Log.d("TAG", "getHeight:" + bind.btn.getHeight()); // 126bind.btn.setVisibility(View.GONE); // 設置消失Log.d("TAG", "getHeight:" + bind.btn.getHeight()); // 126}}, 5000);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Log.d("TAG", "getHeight:" + bind.btn.getHeight()); // 126}}, 8000);}

Xml

        <Buttonandroid:id="@+id/btn"android:visibility="gone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="update"android:textAllCaps="false" />

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/213916.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/213916.shtml
英文地址,請注明出處:http://en.pswp.cn/news/213916.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SQL進階 | 自連接

概述 SQL的自連接是指在一個SQL表中&#xff0c;使用自身表格中的實例進行聯接并查詢的操作。自連接通常使用別名來標識一個表格&#xff0c;在自連接中&#xff0c;表格被視為兩個不同的表格&#xff0c;并分別用不同的別名來標識。然后&#xff0c;在WHERE子句中使用這些別名…

oracle異常:ORA-03297:文件包含在請求的 RESIZE 值以外使用的數據

出現這個問題&#xff0c;主要是在對表空間擴容的時候&#xff0c;擴容的大小<實際數據文件大小 1、擴容的語句 alter database datafile D:\APP\ADMINISTRATOR\ORADATA\ORCL\USER.DBF resize 2G; 2、若何確定擴容大小是否比實際文件大 根據路徑找到文件&#xff0c;查看…

二十、FreeRTOS之Tickless低功耗模式

本節需要掌握以下內容&#xff1a; 1&#xff0c;低功耗模式簡介&#xff08;了解&#xff09; 2&#xff0c; Tickless模式詳解&#xff08;熟悉&#xff09; 3&#xff0c; Tickless模式相關配置項&#xff08;掌握&#xff09; 4&#xff0c;Tickless低功耗模式實驗&…

自定義異步任務管理器和線程

import com.lancoo.common.utils.Threads; import com.lancoo.common.utils.spring.SpringUtils;import java.util.TimerTask; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;/*** 異步任務管理器* * author lancoo*/ public c…

操作系統大會 openEuler Summit 2023即將召開,亮點不容錯過

【12月11日&#xff0c;北京】數字化、智能化浪潮正奔涌而來。操作系統作為數字基礎設施的底座&#xff0c;已經成為推動產業數字化、智能化發展的核心力量&#xff0c;為數智未來提供無限可能。12月15-16日&#xff0c;以“崛起數字時代 引領數智未來”為主題的操作系統大會 &…

〖Python網絡爬蟲實戰?〗- 極驗滑塊介紹(四)

訂閱&#xff1a;新手可以訂閱我的其他專欄。免費階段訂閱量1000 python項目實戰 Python編程基礎教程系列&#xff08;零基礎小白搬磚逆襲) 說明&#xff1a;本專欄持續更新中&#xff0c;訂閱本專欄前必讀關于專欄〖Python網絡爬蟲實戰〗轉為付費專欄的訂閱說明作者&#xff1…

【ArcGIS Pro微課1000例】0049:根據坐標快速定位(創建點位)的常見方法

文章目錄 一、轉到XY1. 閃爍位置2. 平移3. 標記位置二、定位1. 坐標定位2. 添加到圖形3. 添加至要素類三、添加XY坐標四、創建點要素一、轉到XY 舉例:經緯度坐標:113.2583286東, 23.1492340北 。 1. 閃爍位置 輸入坐標,點擊閃爍位置工具,即可在對應的位置出現一個綠色閃爍…

SQL注入漏洞利用

預計更新SQL注入概述 1.1 SQL注入攻擊概述 1.2 SQL注入漏洞分類 1.3 SQL注入攻擊的危害 SQLMap介紹 2.1 SQLMap簡介 2.2 SQLMap安裝與配置 2.3 SQLMap基本用法 SQLMap進階使用 3.1 SQLMap高級用法 3.2 SQLMap配置文件詳解 3.3 SQLMap插件的使用 SQL注入漏洞檢測 4.1 SQL注入…

shiro入門demo

搭建springboot項目&#xff0c;引入以下依賴&#xff1a; <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--單元測試--><depe…

集的增刪操作

集是可以修改的&#xff0c;增加元素可以用add和update函數。刪除可以用pop、discard、remove等函數。 1 增加集里的元素 add函數的參數視為一個整體插入到集里&#xff0c;而update函數傳入的參數是把要傳入的元素拆分&#xff0c;做為個體傳入到集合中。 s set("hell…

.9.png的創建

1、創建.9.png 選中圖片&#xff0c;右擊&#xff0c;選擇Create 9-Patch file&#xff0c;點擊確定會生成一個xxx.9.png的圖片 2、繪制拉伸區域 在圖片的最外邊界繪制拉伸區域&#xff0c;按住鼠標左鍵不放&#xff0c;繪制完成后保存就可以使用了。繪制結果示意如下&…

phpstudy小皮(PHP集成環境)下載及使用

下載 https://www.xp.cn/download.html直接官網下載即可&#xff0c;下載完解壓是個.exe程序&#xff0c;直接點擊安裝就可以&#xff0c;它會自動在D盤目錄為D:\phpstudy_pro 使用 phpMyAdmin是集成的數據庫可視化&#xff0c;這里需要下載一下&#xff0c;在軟件管理-》網站程…

OPPO怎么錄屏?教程來了,讓你成為錄屏達人

“有人知道OPPO怎么錄屏嗎&#xff0c;前陣子剛買的OPPO手機&#xff0c;用起來感覺挺流暢的&#xff0c;功能也很齊全&#xff0c;最近因為工作原因&#xff0c;需要用到錄屏功能&#xff0c;但是我不知道怎么打開&#xff0c;就想問問大伙&#xff0c;OPPO怎么錄屏呀。” 在…

Redis分片集群一步一步全過程搭建

文章目錄 Redis搭建分片集群1. 搭建的分片集群結構2.準備實例和配置&#xff08;1&#xff09;創建目錄&#xff08;2&#xff09;創建配置文件&#xff08;3&#xff09;將這個文件拷貝到每個目錄下&#xff08;4&#xff09;修改每個目錄下的redis.conf&#xff0c;將其中的6…

Yocto 還是 Buildroot,構建自定義嵌入式系統時,您會選擇哪一種?

假設您正在采用集成平板開發新一代大型智能微波爐。這個創意不錯吧&#xff01;現在&#xff0c;您需要構建自定義操作系統&#xff0c;在保證不會燒焦食物&#xff08;更不要燒毀房屋哦&#xff09;的前提下&#xff0c;輔助管理各項事務。除此之外&#xff0c;您還需要創建一…

px? pt? dp? em?rem?vw?vh?ch?ex?這些單位你了解嗎?

目錄 前言 一、常見單位 1、px單位 2、dp單位 3、pt單位 4、百分比% 5、em單位 6、rem單位 7、vw和vh單位 8、ch、ex單位 二、如何換算 1、 pt和px換算 2、px和dp換算 3、em和px換算 4、rem和px換算 三、總結 前言 前端開發在日常設計中除了最常用的 px 以外&…

第二十八章 控制到 XML 模式的映射 - 流類到 XML 類型的映射

文章目錄 第二十八章 控制到 XML 模式的映射 - 流類到 XML 類型的映射將集合屬性映射到 XML 模式 第二十八章 控制到 XML 模式的映射 - 流類到 XML 類型的映射 如果類或屬性基于流&#xff0c;則它將投影為 XML 類型&#xff0c;如下表所示&#xff1a; IRIS 流的 XML 類型 …

使用BeautifulSoup 4和Pillow合并網頁圖片到一個PDF:一種高效的方式來處理網頁圖像

背景 ? 網頁上的培訓材料&#xff0c;內容全是PPT頁面圖片。直接通過瀏覽器打印&#xff0c;會存在只打印第一頁&#xff0c;并且把瀏覽器上無效信息也打印出來情況。但目標是希望將頁面圖片全部打印為pdf形式。 實現方案 利用網頁“另存為”&#xff0c;將頁面內所有圖片資…

官宣!「灣區之光群星演唱會」拉開2024新年音樂華麗序幕!

萬眾期待&#xff0c;群星薈萃&#xff01;青春寶安時尚灣區——灣區之光群星演唱會即將在2024年1月5日閃耀亮相深圳寶安。 華語歌壇巨星天后齊聚一堂&#xff0c;攜手多位實力唱將&#xff0c;共同呈現一場無與倫比的演唱會盛宴&#xff01;在深情而又充滿力量的歌聲之中&…

Linux修復磁盤壞道,重新掛載硬盤

修復磁盤 掛載報錯&#xff1a; [rootlocalhost ~]$ mount /dev/sdb /mnt/mydevmount: /dev/sdb is write-protected, mounting read-only mount: wrong fs type, bad option, bad superblock on /dev/sdb,missing codepage or helper program, or other errorIn some cases …