企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 一.作用: LayoutInflater作用是将layout的xml布局文件实例化为View类对象,LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。 ### 二.获得 LayoutInflater 实例的三种方式 1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater() 2.LayoutInflater inflater = LayoutInflater.from(this); 3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE); 其实,这三种方式本质是相同的,从源码中可以看出: **getLayoutInflater():** Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码: ~~~ public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); } ~~~ 可以看出它其实是调用 LayoutInflater.from(context)。 LayoutInflater.from(context): ~~~ 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; } ~~~ 可以看出它其实调用 context.getSystemService()。 结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。 ### 三.实例化LayoutInflater之后,就要将layout的xml布局文件实例化为View类对象。 1. ~~~ LayoutInflater inflater = getLayoutInflater(); View view=inflater.inflate(R.layout.ID, null); ~~~ 2. ~~~ LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); ~~~ 3. ~~~ LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.ID, null); ~~~ ### 四.inflate 方法 通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下: - public View inflate (int resource, ViewGroup root)  (常用) - inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null。这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。 - public View inflate (XmlPullParser parser, ViewGroup root)  - public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  - public View inflate (int resource, ViewGroup root, boolean attachToRoot) 示意代码: ~~~ <pre name="code" class="java">LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); //EditText editText = (EditText)findViewById(R.id.content);// error EditText editText = (EditText)view.findViewById(R.id.content); ~~~ 对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。 ### 实例: 下面我们就通过一个非常简单的小例子,来更加直观地看一下LayoutInflater的用法。比如说当前有一个项目,其中MainActivity对应的布局文件叫做activity_main.xml,代码如下所示: ~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" > </LinearLayout> ~~~ 这个布局文件的内容非常简单,只有一个空的LinearLayout,里面什么控件都没有,因此界面上应该不会显示任何东西。 那么接下来我们再定义一个布局文件,给它取名为button_layout.xml,代码如下所示: ~~~ <Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" > </Button> ~~~ 这个布局文件也非常简单,只有一个Button按钮而已。 现在我们要想办法,如何通过LayoutInflater来将button_layout这个布局添加到主布局文件的LinearLayout中。根据刚刚介绍的用法,修改MainActivity中的代码,如下所示: ~~~ public class MainActivity extends Activity { private LinearLayout mainLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainLayout = (LinearLayout) findViewById(R.id.main_layout); LayoutInflater layoutInflater = LayoutInflater.from(this); View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null); mainLayout.addView(buttonLayout); } } ~~~ 可以看到,这里先是获取到了LayoutInflater的实例,然后调用它的inflate()方法来加载button_layout这个布局,最后调用LinearLayout的addView()方法将它添加到LinearLayout中。 现在可以运行一下程序,结果如下图所示: ![](https://box.kancloud.cn/2016-02-18_56c5a956452d4.jpg) Button 在界面上显示出来了!说明我们确实是借助LayoutInflater成功将button_layout这个布局添加到LinearLayout中了。 LayoutInflater技术广泛应用于需要动态添加View的时候,比如在ScrollView和ListView中,经常都可以看到 LayoutInflater的身影。 参考: http://www.cnblogs.com/merryjd/archive/2013/01/08/2851092.html http://blog.csdn.net/guolin_blog/article/details/12921889