java layoutinflater_LayoutInflater-使用

本文深入探讨Android的LayoutInflater,解析其用法和原理。通过示例解释了LayoutInflater.inflate()方法的参数作用,展示了如何在不同参数设置下影响视图的创建和添加。通过源码分析,帮助读者理解 LayoutInflater 如何将布局资源转换为视图,并添加到父布局中。最后,提供了实际操作的代码示例以巩固理解。
摘要由CSDN通过智能技术生成

本文主要讲关于LayoutInflater的用法,让读者更好地了解并使用它。

根布局

在讲述正文之前。我们必须了解一个叫做根布局的概念。其实很简单。其实就是ViewGroup的最外面的一层布局,我们随便写一个Xml布局文件,布局文件名为view_new.xml:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Fizz"

android:layout_centerInParent="true"/>

在这个view_new.xml布局中,根元素为RelativeLayout,根布局就是

android:layout_width="match_parent"

android:layout_height="match_parent">

其中的android:layout_width="match_parent" 、android:layout_height="match_parent"其实就是根布局的布局属性,合起来我们统称为布局属性集合。

在使用LayoutInflater中,如果root不为Null,那么LayoutInflater就会将 新视图的根视图属性集合 转换成适配 root根布局的布局属性集合。

概述

虽然网络上对于LayoutInflater有很多的解释文章。但是大多草草带过,读者大多处于一种朦胧的理解状态,并不能真正地融会贯通。本来通过 基本用法、源码解析来讲述它。

我们知道`LayoutInflater``是根据布局资源文件来生成视图层级(包括子视图)的系统服务,获取实例的方式有两种:

LayoutInflater.from(Context context)

(LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

让我们看下第一种方式的内部:

public static LayoutInflater from(Context context) {

LayoutInflater LayoutInflater =

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (LayoutInflater == null) {

throw new AssertionError("LayoutInflater not found.");

}

return LayoutInflater;

}

可以看到,第一种方法也是调用了第二种方法来获取实例,所以两者本身没什么区别。

使用

LayoutInflater使用的方法:

LayoutInflater.inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)。

可以看到,它总共需要三个参数,分别为:

resource:布局资源文件,也就是我们将要根据它来创建新的视图。

root:根布局。可以设置为Null。如果为Null,那新布局(第一个参数)的根布局参数(注意:是新视图的根布局)就不会被设置成任何布局参数,只有等添加到父布局时候,重新赋给这个根布局新的布局参数,并且第三个参数将毫无作用。我们在第三个参数讲解第二个参数在非Null情况。

attachToRoot:是否将新的布局添加到根布局(root),记住这个参数只有在第二个参数非Null的情况才生效。如果此参数为false,那么新的视图不会被添加进根布局,只会将新的视图根布局的布局参数转换成root的根布局参数。如果此参数为true,那么除了将新的视图根布局的布局参数转换成root的根布局参数,LayoutInflater还会调用root.addView(temp, params)方法,将新的视图添加进根布局。

如果看的有点晕。不要着急,实践是唯一的真理。我们来写个demo看看。

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.fizz.myfragment.MainActivity">

android:background="#888888"

android:layout_width="200dp"

android:layout_height="200dp"

android:id="@+id/frame"

android:layout_centerInParent="true"/>

主页面的布局,只有一个居中的FrameLayout布局。等会我们用LayoutInflater生成的布局视图添加到它里面。也就是说此FrameLayout就是根布局。

2a70861fd0fa

主页面.png

view_child.xml

android:layout_width="100dp"

android:layout_height="100dp"

android:orientation="vertical"

android:background="@color/colorAccent">

android:id="@+id/btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮"

android:layout_centerInParent="true"/>

这就是需要创建的视图资源文件,也就是新视图。

2a70861fd0fa

视图资源文件

MainActivity.java

public class MainActivity extends AppCompatActivity {

FrameLayout frameLayout;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

frameLayout = (FrameLayout) findViewById(R.id.frame);

//我们在这里尝试替换不同的参数

// View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child, frameLayout, false);

}

}

主Activity很简单,我们试着添加不同的参数来看印证我们上面的理论。

(1). View view =LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,null);

2a70861fd0fa

结果.png

可以看到,frameLayout并没有将新视图显示出来。因为我们只生成了新视图,并没有将新视图添加到framelayout。囧。。并且!!此时这个view的根布局参数是null!,后面验证。

那我如何把这个新布局添加进framelayout呢?其实很简单:

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child, null);

frameLayout.addView(view);

2a70861fd0fa

inflate(R.layout.view_child,null)

咦?不是说新布局根布局没有布局参数吗?怎么还能添加进父布局吗?其实在frameLayout.addView(view)中有这么一行代码。

LayoutParams params = child.getLayoutParams();

if (params == null) {

params = generateDefaultLayoutParams();

if (params == null) {

throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");

}

}

看到了当子视图的根布局参数为空时,父布局会为子视图生成一个默认的布局参数。FrameLayout重写了generateDefaultLayoutParams这个方法。

@Override

protected LayoutParams generateDefaultLayoutParams() {

return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

}

所以!子布局填充满了父布局。

(2).

View view =LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,frameLayout);

View view =LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,frameLayout,true);(这两个是一样的,实现后面源码再详细讲)

2a70861fd0fa

结果.png

我们可以看到,新视图被添加到framelayout中,这就是我想要的结果。一行代码就将生成布局,并且将布局添加进了父布局。先别嘚瑟。其实这行代码主要做了两件事情,一是新视图的根布局参数转换为适合frameLayout的布局参数,然后将子视图添加进了frameLayout(根布局也是父布局)。

(3). `View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,frameLayout,false);

2a70861fd0fa

结果.png

这个结果和第一种方式是一样。但是!!在这种方式中新视图的根布局参数转换为适合frameLayout的布局参数,而第一种方式的布局参数却是Null**。

我们将它添加到父布局:

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,frameLayout,false);

frameLayout.addView(view);

2a70861fd0fa

inflate(R.layout.view_child,frameLayout,false)

果然。此时的新视图是有根布局参数的。所以父布局没有对它设置默认的布局参数。

如果只想掌握LayoutInflater用法的读者,读到这里就可以点击关闭了。

后面我们将深入LayoutInflater源码。"LayoutInflater-源码分析"来彻底征服它~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.IllegalStateException: ScrollView can host only one direct child at androidx.core.widget.NestedScrollView.addView(NestedScrollView.java:507) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1131) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1130) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.inflate(LayoutInflater.java:686) at android.view.LayoutInflater.inflate(LayoutInflater.java:505) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:443) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:722) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$9(RenderTask.java:879) at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$3.run(RenderExecutor.kt:194) at com.android.tools.idea.rendering.RenderExecutor$PriorityRunnable.run(RenderExecutor.kt:292) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值