Android View.inflate 和 LayoutInflater.from(this).inflate 的区别

2023-12-09 22:30

本文主要是介绍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

这篇关于Android View.inflate 和 LayoutInflater.from(this).inflate 的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/475295

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle