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)


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 值。

Demo:

以下我们就通过一个很easy的小样例,来更加直观地看一下LayoutInflater的使用方法。

比方说当前有一个项目。当中MainActivity相应的布局文件叫做activity_main.xml,代码例如以下所看到的:

1.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2.android:id="@+id/main_layout"
3.android:layout_width="match_parent"
4.android:layout_height="match_parent"
>
5. 
6.</LinearLayout>

这个布局文件的内容很easy。仅仅有一个空的LinearLayout,里面什么控件都没有,因此界面上应该不会显示不论什么东西。

那么接下来我们再定义一个布局文件,给它取名为button_layout.xml,代码例如以下所看到的:

2.android:layout_width="wrap_content"
3.android:layout_height="wrap_content"
4.android:text="Button"
>
5. 
6.</Button>

这个布局文件也很easy。仅仅有一个Buttonbutton而已。

如今我们要想办法,怎样通过LayoutInflater来将button_layout这个布局加入到主布局文件的LinearLayout中。

依据刚刚介绍的使用方法,改动MainActivity中的代码。例如以下所看到的:

01.public
class MainActivity extends
Activity {
02. 
03.private
LinearLayout mainLayout;
04. 
05.@Override
06.protected
void onCreate(Bundle savedInstanceState) {
07.super.onCreate(savedInstanceState);
08.setContentView(R.layout.activity_main);
09.mainLayout = (LinearLayout) findViewById(R.id.main_layout);
10.LayoutInflater layoutInflater = LayoutInflater.from(this);
11.View buttonLayout = layoutInflater.inflate(R.layout.button_layout,
null);
12.mainLayout.addView(buttonLayout);
13.}
14. 
15.}

能够看到。这里先是获取到了LayoutInflater的实例。然后调用它的inflate()方法来载入button_layout这个布局,最后调用LinearLayout的addView()方法将它加入到LinearLayout中。

如今能够执行一下程序,结果例如以下图所看到的:

Button 在界面上显示出来了!

说明我们确实是借助LayoutInflater成功将button_layout这个布局加入到LinearLayout中了。 LayoutInflater技术广泛应用于须要动态加入View的时候,比方在ScrollView和ListView中。常常都能够看到 LayoutInflater的身影。

动态设置布局LayoutInflater的更多相关文章

  1. Android 动态改变布局属性RelativeLayout.LayoutParams.addRule()

    我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...

  2. 代码中动态改变布局属性RelativeLayout.LayoutParams.addRule()

    我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...

  3. 动态设置 view 在布局中位置

    一.概述 有时项目需要动态设置一个 底部列表,比如 popupwindow ,listview 底部显示 ,所以记录一下 此处, android.support.v7.widget.CardView ...

  4. Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解

    废话不多说,本文将会层层深入给大家讲解如何动态的生成一个完整的界面. 本文内容: Java代码中动态生成View Java代码中动态设置View的位置,以及其他的属性 LayoutParams详解 一 ...

  5. android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)

    效果图 :                         分别是第一页.第二页.第三页,随手截的图,不整齐,勿见怪.开始走了弯路,废了不少时间. 思路如下: 1.用ViewPager实现左右分页滑动 ...

  6. 怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)

    首先扯点别的:我应经连续上了两个星期的班了,今天星期一.是第三个周.这个班上的也是没谁了.近期老是腰疼. 预计是累了.近期也没跑步.今天下班继续跑起. 这篇文章讲一讲怎样在一个布局文件里动态加在一个布 ...

  7. 【Android疑难杂症】GridView动态设置Item的宽高导致第一个Item不响应或显示不正常的问题

    前言 这个问题在之前做一个盒子项目时遇到过,最近又遇到了,使用GridView遇到的非常奇葩的问题,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnb ...

  8. 实现ScrollView中包含ListView,动态设置ListView的高度

    ScrollView 中包含 ListView 的问题 : ScrollView和ListView会冲突,会导致ListView显示不全 <?xml version="1.0" ...

  9. Android textView 动态设置代码字号大小,支持单位选项 dp,sp or px

    setTextSize(TypedValue.COMPLEX_UNIT_PX,22); //22像素 setTextSize(TypedValue.COMPLEX_UNIT_SP,22); //22S ...

随机推荐

  1. ThinkPHP表单令牌验证功能详细介绍

    注:TP版本为3.1.3 在ThinkPHP框架下,两次提交同一个表单,比如提交信息后在浏览器点击后退退回上次的页面,重新点击提交按钮,就会提示“表单令牌错误”的信息. ThinkPHP新版内置了表单 ...

  2. Google开源库-Volley

    Android平台的网络通信库,使是网通信 更快,更简单,更健壮 适合场景: 数据量不大,通信 频繁. 大数据,流媒体是不适合使用的 * 它主要是帮我们载入和缓存从远程网络加载的图片    * 所有的 ...

  3. Maya pywin32

    Maya 2011 – 2013 64-bit: maya-64-bit-pywin32.zipMaya 2011 – 2013 32-bit: maya-32-bit-pywin32.zipMaya ...

  4. Cinder-1 TinderBox

    Cinder:http://libcinder.org/,当前版本是0.8.5,代码托管位置:https://github.com/cinder/Cinder.git 下载Cinder之后,其目录结构 ...

  5. C# Http POST get

    using System.IO;using System.Net; /// <summary>        /// HttpWebRequest发送Post请求     /// < ...

  6. Uva_11427 Expect the Expected

    题目链接 题意: 你玩纸牌, 如果当天晚上你赢的局数比例 大于 p, 就去睡觉, 第二天继续. 如果小于等于p, 就去睡觉, 并且以后都不玩了. 每晚最多玩n局, 每局赢的概率为p , 求玩的天数的期 ...

  7. 完全卸载mysql 停止服务、卸载相关程序、删除注册表

    本节主要介绍了完全卸载mysql的具体步骤包括停止服务.卸载相关程序.删除注册表等等   1. 停止服务MySQL 2. 卸载mysql相关的程序 3. 删除注册表(运行->regedit),m ...

  8. iptables 配置需要保存

    iptables-save > /root/myiptables 将iptables规则导入到文件/root/myiptablse iptables-restore < /root/myi ...

  9. Hadoop下各技术应用场景

    数据采集和DataFlow 对于数据采集主要分为三类,即结构化数据库采集,日志和文件采集,网页采集.对于结构化数据库,采用Sqoop是合适的,可以实现结构化数据库中数据并行批量入库到hdfs存储.对于 ...

  10. Android 判断是否联网 是否打开上网

    ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); ...