LayoutInflater-使用

作者: miraclehen | 来源:发表于2017-07-25 11:11 被阅读221次

转载请注明出处 http://www.jianshu.com/p/2a70861fd0fa (作者:韩栋)
由于本人水平有限,欢迎拍砖。

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

根布局

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Fizz"
        android:layout_centerInParent="true"/>

</RelativeLayout>

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>

其中的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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fizz.myfragment.MainActivity">

    <FrameLayout
        android:background="#888888"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/frame"
        android:layout_centerInParent="true"/>
</RelativeLayout>

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

主页面.png

view_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:background="@color/colorAccent">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_centerInParent="true"/>
</RelativeLayout>

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

视图资源文件

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);

结果.png
可以看到,frameLayout并没有将新视图显示出来。因为我们只生成了新视图,并没有将新视图添加到framelayout。囧。。并且!!此时这个view的根布局参数是null!,后面验证
那我如何把这个新布局添加进framelayout呢?其实很简单:
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child, null);
frameLayout.addView(view);
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);(这两个是一样的,实现后面源码再详细讲)

结果.png
我们可以看到,新视图被添加到framelayout中,这就是我想要的结果。一行代码就将生成布局,并且将布局添加进了父布局。先别嘚瑟。其实这行代码主要做了两件事情,一是新视图的根布局参数转换为适合frameLayout的布局参数,然后将子视图添加进了frameLayout(根布局也是父布局)。
(3). `View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,frameLayout,false); 结果.png
这个结果和第一种方式是一样。但是!!在这种方式中新视图的根布局参数转换为适合frameLayout的布局参数,而第一种方式的布局参数却是Null**。
我们将它添加到父布局:
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_child,frameLayout,false);
frameLayout.addView(view);
inflate(R.layout.view_child,frameLayout,false)

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

如果只想掌握LayoutInflater用法的读者,读到这里就可以点击关闭了。
后面我们将深入LayoutInflater源码。"LayoutInflater-源码分析"来彻底征服它~

下文:"LayoutInflater-源码分析"

相关文章

网友评论

    本文标题:LayoutInflater-使用

    本文链接:https://www.haomeiwen.com/subject/lmdekxtx.html