学习Android遇到的问题——LayoutInflater

前言

对于像我这样的android初学者来说,LayoutInflater是个很模糊的概念,I can’t feel it!我只知道在自定义控件的时候构造函数中会用到LayoutInflater.from(mContext).inflater(layout_id, this);还有重写 适配器布局会用到LayoutInflater.from(getContext()).inflater(layout_id, null)

从字面的意思来看,Layout是布局的意思,Inflater是充气器的意思;连起来就是布局充气器,或者布局填充器,也有人说是布局加载器。

而我现在对于LayoutInflater的理解也仅仅是他可以将xml布局实例化为View对象。

引入问题

假设,你要用到一个View(比如一个设计好的登陆界面),你要经常用,而且界面布局是固定的。你会怎么做呢?我可不可以先在xml文件中先布局好,然后再把这个xml布局放到一个View对象上呢?

看到这里,你可能会说我用setContentView(R.layout.activity_main);不就好了,直接就可以加载啊。是的,使用setContentView可以在Activity中动态切换显示的View,这样,不需要多个Activity就可以显示不同的界面。但如果使用该方法切换view,在切换后再切换回,无法显示切换前修改后的样子,也就是说,相当于重新显示一个view,并非是把原来的view隐藏后再显示。

这个时候,LayoutInflater就登场,setContentView是个多态方法,我们可以先用LayoutInflater把布局xml文件实例化为View对象,再通过setContentView(View view)方法来切换视图。因为所有对View的修改都保存在View对象里,所以,当切换回原来的view时,就可以直接显示原来修改后的样子。

那么具体怎么用,怎么转化呢?

具体方法

获得LayoutInflater实例

首先我们要知道获取LayoutInflater实例,有三种方式:

1、LayoutInflater mInflater = getLayoutInflater();//调用Activity的getLayoutInflater()

2、LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

3、LayoutInflater mInflater = LayoutInflater.from(context);
它们的本质都是一样的。一般来说(就我来说),第三种方法最常用。
下面我就用第三种方法举个例子。

LayoutInflater的使用

这里我新建了两个xml文件:
activity_main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<TextView
android:id="@+id/tv_greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Hello Main_XML!"
android:textSize="30sp"/>

<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_greeting"
android:layout_centerHorizontal="true"
android:text="Goto"/>

</RelativeLayout>

activity_first.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Hello First_XMLy!"
android:textSize="30sp"/>

</LinearLayout>

在MainActivity.java代码中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

public class MainActivity extends AppCompatActivity
{

private Button button;
private View mView;

@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn_next);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{

mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_first, null);

setContentView(mView);

}
});
}
}

LayoutInflater.from(MainActivity.this)api文档中的解释是“Obtains the LayoutInflater from the given context.”即从LayoutInflater中获取到LayoutInflater实例,也可以理解为把指定的布局资源注射到自定的容器,或加载布局管理器。
inflate(R.layout.activity_first, null)意思是将activity_first.xml布局转换为view对象。

当点击按钮替换原来的布局后,按返回按钮就会退出程序。这就相当于用activity_first.xml替换原来的activity_main.xml

搜索了一下LayoutInflater的博文,果然无独有偶,有很多写的不错的文章,写下这篇文章也是站在了他们的肩膀上。他们的博文也提到了LayoutInflater更多的用法。

LayoutInflater那些事儿

Android LayoutInflater详解

LayoutInflater

讲的更加细致深入的—>也是《第一行代码》的作者
Android LayoutInflater原理分析,带你一步步深入了解View(一)
一分析Android源码就蒙了QAQ…