LayoutInflater类在应用程序中比较实用,可以叫布局填充器,也可以成为打气筒,意思就是将布局文件填充到自己想要的位置,LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化这个XML文件成为一个View,有点类似于类似于findViewById(),但是findViewById()是找xml布局文件下的具体widget控件(Button、TextView)。对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;对于一个已经载入的界面,使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater实例方式

首先简单看下三种简单的实例方式:

	LayoutInflater layoutInflater = getLayoutInflater();
LayoutInflater layoutInflater2 = LayoutInflater.from(this);
LayoutInflater layoutInflater3 = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

第一种getLayoutInflater方式:

   public LayoutInflater getLayoutInflater() {
return getWindow().getLayoutInflater();
}

第二种实现方式:

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

 后两种调用的都是getSystemService,第一种也是~

LayoutInflater实现Demo

LayOutInflater填充布局的时候有四种方法,Demo使用第一种:

resource:需要加载布局文件的id,需要将这个布局文件中加载到Activity中来操作。

root:inflate()会返回一个View对象如果root不为null,就将这个root作为根对象返回,作为这个xml文件的根视图,如果是null,那么这个xml文件本身就是一个根视图:

public View inflate (int resource, ViewGroup root)
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)

 看下实现的结果:

在主窗体中定义一个按钮,这个代码就不贴了~直接写点击事件的处理吧:

定义一个Item文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/inflate_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="测试"/> </LinearLayout>

  点击事件的处理:

		LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.item, null);
TextView text = (TextView) view.findViewById(R.id.inflate_txt);
text.setText("http://www.cnblogs.com/xiaofeixiang");
text.setTextSize(18);
text.setTextColor(Color.RED);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();

Android数据填充器LayoutInflater的更多相关文章

  1. Android LayoutInflater布局填充器

    Android LayoutInflater布局填充器 把一份xml布局文件转为View对象,这就是layoutinflater的作用. 对于一个没有被载入或者想要动态载入的界面,都需要使用Layou ...

  2. 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)

    前言 关键字:Vitamio.VPlayer.Android播放器.Android影音.Android开源播放器 本章节把Android万能播放器本地播放的主要功能(缓存播放列表和A-Z快速查询功能) ...

  3. Android Multimedia框架总结(八)Stagefright框架之AwesomePlayer及数据解析器

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼:http://blog.csdn.net/hejjunlin/article/details/52503057 前言:前面一篇分析了medi ...

  4. 使用Vitamio打造自己的Android万能播放器(6)——在线播放(播放列表)

    前言 新版本的VPlayer由设计转入开发阶段,预计开发周期为一个月,这也意味着新版本的Vitamio将随之发布,开发者们可以和本系列文章一样,先开发其他功能.本章内容为"在线视频播放列表& ...

  5. 使用Vitamio打造自己的Android万能播放器(5)——在线播放(播放优酷视频)

    前言 为了保证每周一篇的进度,又由于Vitamio新版本没有发布, 决定推迟本地播放的一些功能(截图.视频时间.尺寸等),跳过直接写在线播放部分的章节.从Vitamio的介绍可以看得出,其支持http ...

  6. [android] 数据的异步加载和图片保存

    把从网络获取的图片数据保存在SD卡上, 先把权限都加上 网络权限 android.permission.INTERNET SD卡读写权限 android.permission.MOUNT_UNMOUN ...

  7. Android VLC播放器二次开发3——音乐播放(歌曲列表+歌词同步滚动)

    今天讲一下对VLC播放器音频播放功能进行二次开发,讲解如何改造音乐播放相关功能.最近一直在忙着优化视频解码部分代码,因为我的视频播放器需要在一台主频比较低的机器上跑(800M主频),所以视频解码能力受 ...

  8. Android VLC播放器二次开发2——CPU类型检查+界面初始化

    上一篇讲了VLC整个程序的模块划分和界面主要使用的技术,今天分析一下VLC程序初始化过程,主要是初始化界面.加载解码库的操作.今天主要分析一下org.videolan.vlc.gui.MainActi ...

  9. Android数据展示之ListView

    Android应用程序中经常需要使用ListView展示数据,一个ListView通常将数据填充到布局文件中,在填充的过程中需要知道连接数据与ListView的适配器.适配器是一个连接数据和Adapt ...

随机推荐

  1. 使用 Python 可以做什么?

    翻译自 <Python学习手册(第5版)> Systems Programming Python 对操作系统服务的内置接口使其非常适合编写可移植.可维护的系统管理工具和实用程序 utili ...

  2. 美团外卖Android平台化的复用实践

    美团外卖平台化复用主要是指多端代码复用,正如美团外卖iOS多端复用的推动.支撑与思考文章所述,多端包含有两层意思:其一是相同业务的多入口,指美团外卖业务需要在美团外卖App(下文简称外卖App)和美团 ...

  3. XShell通过中转服务器直接连接目标服务器

    最近由于公司生产环境的变化,使得我们不能使用自己的机器连接到生产环境去,而是要通过跳板机中转才可以连接.于是今天尝试使用 XShell 通过跳板机直接转接到生产环境. 一.使用代理方式 首先填写连接信 ...

  4. python调用matlab

    官网链接: https://ww2.mathworks.cn/help/matlab/matlab_external/call-user-script-and-function-from-python ...

  5. python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐

    我使用的python版本为:3.3.2 如果你对python中tkinter模块的菜单操作不是很了解,你可以看看: python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推 ...

  6. tyvj 1044 数字三角形 记忆化搜索

    数字三角形 Time Limit: 1 Sec  Memory Limit: 162 MB 题目连接 http://www.tyvj.cn/p/1044 Description 示出了一个数字三角形. ...

  7. git pull的时候出错: Git Couldn't reserve space for cygwin's heap

    具体: 1. 运行CMD,以管理员身份打开 2. 运行:rebase.exe -b 0x50000000 msys-1.0.dll 再次git pull的时候,不再报错 转自:http://doc.o ...

  8. MyBatis连接SQL Server的关键点

    一.Maven包配置 <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> < ...

  9. MySQL的mysql.sock文件作用(转)

    mysql.sock是可用于本地服务器的套接字文件.它只是另一种连接机制. 不包含任何数据,但仅用于从客户端到本地服务器来进行交换数据.

  10. Tasker to proximity screen off

    If you are using proximity screen off pro or smart screen off, you may know how convenient it is to ...