Android View.inflate 和 LayoutInflater.from(this).inflate 的區別

前言

兩個都是布局加載器,而View.inflate是對 LayoutInflater.from(context).inflate的封裝,功能相同,案例使用了dataBinding。

View.inflate(context, layoutResId, root)

LayoutInflater.from(context).inflate(layoutResId, root, false)

區別

因為View.inflate(context,layoutResId,root)? LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) 少了一個attachToRoot參數(是否將layoutResId添加到某個View中,作為其子View)。

在使用View.inflate(context,layoutResId,root)?時,如果root(父View)是null,會導致layoutResId布局中聲明的寬高 + 外邊距參數,失效。

核心條件就是root(父View)是不是null。

案例

1、使用View.inflate(context,layoutResId,root)?root不為null

    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());// 子布局:R.layout.app_layout_text    // 父布局:bind.boxView.inflate(this,R.layout.app_layout_text,bind.box);View.inflate(this,R.layout.app_layout_text,bind.box);View.inflate(this,R.layout.app_layout_text,bind.box);}

2、使用LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) ?root不為null,且attachToRoot是true

    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());// 子布局:R.layout.app_layout_text    // 父布局:bind.boxLayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);}

兩種方式效果相同,寬高 + 外邊距 都有效

3、使用View.inflate(context,layoutResId,root)?root為?null

    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());// 子布局:R.layout.app_layout_text    // 父布局:bind.boxView view = View.inflate(this, R.layout.app_layout_text, null);View view2 = View.inflate(this, R.layout.app_layout_text, null);View view3 = View.inflate(this, R.layout.app_layout_text, null);bind.box.addView(view);bind.box.addView(view2);bind.box.addView(view3);}

4、使用LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) ?root為?null,且attachToRoot是false

    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());// 子布局:R.layout.app_layout_text    // 父布局:bind.boxView view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);bind.box.addView(view);bind.box.addView(view2);bind.box.addView(view3);}

兩種方式效果相同,寬高 + 外邊距 都失效了,寬/高?變成wrap_content一點要記住這點!!!是變成wrap_content

至于為什么layoutResId布局寬度和父View一樣,當子View失去自身LayoutParams(布局參數)后,父View會自動調整子View的寬高屬性,下面會講先忽略

5、如果不想將layoutResId布局添加到父View中,同時又不想丟失layoutResId布局中聲明的參數,LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot)這樣寫可以做到,root不為null,但是attachToRootfalse

    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());// 子布局:R.layout.app_layout_text    // 父布局:bind.boxView view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);bind.box.addView(view);bind.box.addView(view2);bind.box.addView(view3);}

效果

6、而View.inflate(context,layoutResId,root)?目前為止無法做到,因為它少了一個attachToRoot參數(是否將layoutResId添加到某個View中,作為其子View),以后說不準會有這個參數的重載方法。

7、案例文件:shape_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><stroke android:color="@color/color_303133" android:width="1dp"/>
</shape>

8、案例文件:app_layout_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="300dp"android:layout_height="200dp"android:layout_marginBottom="20dp"android:background="@drawable/shape_border"android:paddingLeft="20dp"android:paddingTop="50dp"android:text="測試" />

9、案例文件: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:id="@+id/box"android:orientation="vertical"android:background="@color/color_14F9230A"android:layout_width="match_parent"android:layout_height="match_parent"></LinearLayout></layout>

10、案例文件: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());// View.inflate(this,R.layout.app_layout_text,bind.box);// View.inflate(this,R.layout.app_layout_text,bind.box);// View.inflate(this,R.layout.app_layout_text,bind.box);// View view = View.inflate(this, R.layout.app_layout_text, null);// View view2 = View.inflate(this, R.layout.app_layout_text, null);// View view3 = View.inflate(this, R.layout.app_layout_text, null);// bind.box.addView(view);// bind.box.addView(view2);// bind.box.addView(view3);// LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);// LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);// LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);// View view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);// View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);// View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);// bind.box.addView(view);// bind.box.addView(view2);// bind.box.addView(view3);// View view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);// View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);// View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);// bind.box.addView(view);// bind.box.addView(view2);// bind.box.addView(view3);}

源碼解析

View.inflate源碼,還是調用的LayoutInflater.from(context).inflate

    public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {LayoutInflater factory = LayoutInflater.from(context);return factory.inflate(resource, root);}

LayoutInflater.java源碼

第一判斷條件是root(父布局)是否為null,第二判斷條件就是attachToRoot,View.inflate沒有這個參數。

    ... ... View result = root;... ... if (root != null) {// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs);if (!attachToRoot) {// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params);}}... ... return result;

父View自動調整子View的寬高

當子View?失去或沒有 自身LayoutParams(布局參數)后,父View會自動調整子View的寬高。

布局類型不同,子View寬高值也不同,說幾個常用布局:

FrameLayout:寬?/?高 都會變成match_parent

RelativeLayout 和?ConstraintLayout 一樣,寬?/?高 都會變成wrap_content

LinearLayout 設置vertical(垂直方向):變成match_parent變成wrap_content

LinearLayout 設置horizontal(水平方向):寬 /?高 都會變成wrap_content

總結

只有在實例化layoutResId布局時,而又不想 作為子View、不想丟失聲明的參數,它倆才會有使用區別。

順便說一下返回值layoutResId布局作為子View時,返回的是父布局View,反之返回的是layoutResId布局View,這一點它們是一樣的。

View view = View.inflate(this, R.layout.app_layout_text, bind.box);Log.d("TAG","父布局LinearLayout:"+(view instanceof LinearLayout)); // trueLog.d("TAG","當前布局TextView:"+(view instanceof TextView)); // falseView view2 = View.inflate(this, R.layout.app_layout_text, null);Log.d("TAG","父布局LinearLayout:"+(view2 instanceof LinearLayout)); // falseLog.d("TAG","當前布局TextView:"+(view2 instanceof TextView)); // trueView view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);Log.d("TAG", "父布局LinearLayout:" + (view3 instanceof LinearLayout)); // trueLog.d("TAG", "當前布局TextView:" + (view3 instanceof TextView)); // falseView view4 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);Log.d("TAG", "父布局LinearLayout:" + (view4 instanceof LinearLayout)); // falseLog.d("TAG", "當前布局TextView:" + (view4 instanceof TextView)); // true

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

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

相關文章

【JS】JS數組添加元素的三種方法

> 1、push() 方法可向數組的末尾添加一個或多個元素&#xff0c;并返回新的長度。 > 2、unshift()方法可向數組的開頭添加一個或更多元素&#xff0c;并返回新的長度。 > 3、splice() 方法向/從數組中添加/刪除項目&#xff0c;然后返回被刪除的項目。1、push() 方法…

nodejs+vue+微信小程序+python+PHP的黃山旅游景點購票系統設計與實現-計算機畢業設計推薦

本文首先對該系統進行了詳細地描述&#xff0c;然后對該系統進行了詳細的描述。管理人員增加了系統首頁、個人中心、用戶管理、景點分類管理、景點簡介管理、旅游路線管理、文章分類管理、公告文章管理、系統管理理等功能。黃山旅游景點購票系統是根據當前的現實需要&#xff0…

線程池的原理和基本使用~

線程池的基本原理&#xff1a; 無論是之前在JavaSE基礎中&#xff0c;我們學習過的常量池&#xff0c;還是在操作數據庫時&#xff0c;我們學習過數據庫連接池&#xff0c;以及接下來要學習的線程池&#xff0c;均是一種池化思想&#xff0c;其目的就是為了提高資源的利用率&a…

mysql 鏈接超時的幾個參數詳解

mysql5.7版本中&#xff0c;先查看超時設置參數&#xff0c;我們這里只關注需要的超時參數&#xff0c;并不是全都講解 show variables like %timeout%; connect_timeout 指的是連接過程中握手的超時時間,在5.0.52以后默認為10秒&#xff0c;之前版本默認是5秒&#xff0c;主…

【vscode寫vue代碼是白色怎么辦】

【vscode寫vue代碼是白色怎么辦】 在插件列表中搜索Vetur 安裝即可

Web學習路線

閱讀前請看一下&#xff1a;我是一個熱衷于記錄的人&#xff0c;每次寫博客會反復研讀&#xff0c;盡量不斷提升博客質量。文章設置為僅粉絲可見&#xff0c;是因為寫博客確實花了不少精力。希望互相進步謝謝&#xff01;&#xff01; 文章目錄 閱讀前請看一下&#xff1a;我是…

Redis 命令全解析之 Hash類型

文章目錄 ?介紹?命令?RedisTemplate API?應用場景 ?介紹 Hash類型&#xff0c;也叫散列&#xff0c;其value是一個無序字典&#xff0c;類似于Java中的 HashMap 結構。 String結構是將對象序列化為JSON字符串后存儲&#xff0c;當需要修改對象某個字段時很不方便&#xf…

降維技術——PCA、LCA 和 SVD

一、說明 降維在數據分析和機器學習中發揮著關鍵作用&#xff0c;為高維數據集帶來的挑戰提供了戰略解決方案。隨著數據集規模和復雜性的增長&#xff0c;特征或維度的數量通常變得難以處理&#xff0c;導致計算需求增加、潛在的過度擬合和模型可解釋性降低。降維技術通過捕獲數…

用隊列實現棧

問題描述&#xff1a; 請你僅用兩個隊列實現一個后入先出&#xff08;LIFO&#xff09;的棧&#xff0c;并支持普通隊列的全部四種操作&#xff08;push、top、pop和empty&#xff09;。 實現MyStack類&#xff1a; void push(int x) 將元素x壓入棧頂。int pop()移除并返回棧頂…

java中線程的狀態是如何轉換的?

在 Java 中&#xff0c;線程有幾種狀態&#xff0c;主要包括 NEW&#xff08;新建&#xff09;、RUNNABLE&#xff08;可運行&#xff09;、BLOCKED&#xff08;阻塞&#xff09;、WAITING&#xff08;等待&#xff09;、TIMED_WAITING&#xff08;計時等待&#xff09;、和 TE…

Vue學習筆記-Vue3中的計算屬性與監視屬性

computed函數 import {reactive,computed} from vue export default {name: "DemoVue",setup(){//數據定義let person reactive({firstName : 李,lastName : 四,age:18,})//計算屬性定義-簡寫形式person.fullName computed(()>{return person.firstName-person…

手寫 Promise:深入理解異步編程的基石

手寫 Promise&#xff1a;深入理解異步編程的基石 本文將帶您逐步實現一個簡單的 Promise&#xff0c;以幫助您深入理解異步編程的基本概念。通過自己動手編寫 Promise 的過程&#xff0c;您將更好地理解 Promise 的工作原理和常見用法&#xff0c;并能夠應用于實際項目中。 …

什么是網站劫持

網站劫持是一種網絡安全威脅&#xff0c;它通過非法訪問或篡改網站的內容來獲取機密信息或者破壞計算機系統。如果您遇到了網站劫持問題&#xff0c;建議您立即聯系相關的安全機構或者技術支持團隊&#xff0c;以獲得更專業的幫助和解決方案。

探索 HTTPS:保障網絡通信的安全性

引言 HTTPS&#xff08;HyperText Transfer Protocol Secure&#xff09;是一種安全的通信協議&#xff0c;用于在網絡上安全地傳輸數據。它是基于 HTTP 協議的擴展&#xff0c;通過加密通信實現了數據的保護和安全性。 功能介紹 加密數據傳輸&#xff1a; 使用加密技術對數…

Prism框架快速注冊帶有特性標簽的類型

前言 最近用Prims框架,真的是懶得手動注冊各種類型,不利于團隊開發工作,各種dll強耦合,后期維護還麻煩,這次我們帶來了一個快速注冊的類來快速提高開發效率。重點用到的就是通過反射出dll里面的類型,然后根據特性或者類型過濾來完成快速注冊的功能。 代碼 using Prism…

Angular 進階之四:SSR 應用場景與局限

應用場景 內容豐富&#xff0c;復雜交互的動態網頁&#xff0c;對首屏加載有要求的項目&#xff0c;對 seo 有要求的項目&#xff08;因為服務端第一次渲染的時候&#xff0c;已經把關鍵字和標題渲染到響應的 html 中了&#xff0c;爬蟲能夠抓取到此靜態內容&#xff0c;因此更…

【面試專題】MySQL篇①

1.MySQL中&#xff0c;如何定位慢查詢? ①介紹一下當時產生問題的場景&#xff08;我們當時的一個接口測試的時候非常的慢&#xff0c;壓測的結果大概5秒鐘&#xff09; ②我們系統中當時采用了運維工具&#xff08; Skywalking &#xff09;&#xff0c;可以監測出哪個接口…

PostgreSQL從小白到高手教程 - 第38講:數據庫備份

PostgreSQL從小白到專家&#xff0c;是從入門逐漸能力提升的一個系列教程&#xff0c;內容包括對PG基礎的認知、包括安裝使用、包括角色權限、包括維護管理、、等內容&#xff0c;希望對熱愛PG、學習PG的同學們有幫助&#xff0c;歡迎持續關注CUUG PG技術大講堂。 第38講&#…

running小程序重要技術流程文檔

一、項目文件說明&#xff1a; &#xff08;注&#xff1a;getMyMoney無用已刪除&#xff09; 二、重要文件介紹 1.reinfo.js&#xff1a;位于utils文件下&#xff0c;該文件封裝有統一的請求URL&#xff0c;和請求API同意封裝供頁面調用&#xff1b;調用時候需要在頁面上先…

【C語言】操作符詳解(一):進制轉換,原碼,反碼,補碼

目錄 操作符分類 2進制和進制轉換 2進制轉10進制 10進制轉2進制 2進制轉8進制和16進制 2進制轉8進制 2進制轉16進制 原碼、反碼、補碼 操作符分類 操作符中有一些操作符和二進制有關系&#xff0c;我們先鋪墊一下二進制的和進制轉換的知識。 2進制和進制轉換 其實我們經…