android LayoutInflater 笔记
LayoutInflater类用于查找布局文件并实例化。用于动态将布局加入界面中。
参考链接
http://blog.csdn.net/guolin_blog/article/details/12921889
http://blog.sina.com.cn/s/blog_5da93c8f0100xm6n.html
分析
- 生成LayoutInflater实例2种方法
LayoutInflater layoutInflater = LayoutInflater.from(context);
或者
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
其中context是Activity的上下文内容。如果在activity中可以通过this表示,或者可通过getApplicationContext()获取。
- setContentview()区别
使用setContentView()函数设置了布局,会马上显示在UI中。而LayoutInflater的实例却不会。
- findViewById()
findViewById()只能查找setContentView()中设置的layout布局中的控件。
如果setContentView()所设置的布局中没有所要访问的控件,而在其他布局文件中,直接使用findViewById()访问会出错。
需要先通过LayoutInflater类现将对应的布局实例化之后,并添加到布局中(不添加也看不到控件).再通过findViewById()。
Example
根据参考链接的源码,记录于此。
- activity_main.xml
主要的布局文件,一个RelativeLayout,里面什么也没有。
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/main_layout"
tools:context="com.example.inflatetest.MainActivity">
</RelativeLayout>
- button_layout.xml
将要将这个布局动态添加到activity_main.xml中。里面有LinearLayout和Button。
<?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">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btn"
android:text="button"
/>
</LinearLayout>
- AndroidManifest.xml
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.inflatetest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- ManiActivity.java
主要代码如下。
使用首先实例化LayoutInflater,
然后使用inflate()加载要添加的布局文件
最后使用addView()添加到activity_main.xml中。
之后使用findViewById()查找button才会成功。
package com.example.inflatetest;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RelativeLayout mainLayout;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建LayoutInflater实例
LayoutInflater layoutInflater = LayoutInflater.from(this);
// 另外一种方法生成LayoutInflater实例。
// LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 通过LayoutInflater获取布局文件
View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);
// 将获取的布局添加到当前布局中。
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
mainLayout.addView(buttonLayout);
// 使用inflater获取布局并添加之后,下面两种方法都可以。
// btn = (Button) buttonLayout.findViewById(R.id.btn);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "button click", Toast.LENGTH_SHORT).show();
}
});
}
}
显示效果:

Tony Liu
2017-3-17, Shenzhen
android LayoutInflater 笔记的更多相关文章
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
- android 应用笔记
android 应用笔记 android 应用笔记 小书匠 Android 综合教程 Android常用技巧 安卓系统架构 安卓源码开发 安卓驱动 Linux内核 安卓应用开发 Java 教程 tic ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
随机推荐
- Oschina 安卓client源代码学习之中的一个
今天主要研究一下两个功能 (1)双击返回键退出程序 (2)接近完美地退出程序 (1) 在非常多应用程序里都有一个功能,就是点击返回键,之后提示你再点击返回键就退出程序. 之前一直非常好奇这是怎么实现的 ...
- Python isalnum() 方法
描述 Python isalnum() 方法检测字符串是否由字母和数字组成. 语法 isalnum()方法语法: S.isalnum() 参数 无. 返回值 如果字符串至少有一个字符并且所有字符都是字 ...
- 分享Memcached shell启动停止脚本
注意:要使用这个shell,必须先成功建立memcache环境 1>建立memcached文件和权限 [root@luozhonghua ~]# touch /etc/init.d/memcac ...
- VS2012开发cocos游戏遇到问题汇总
1.编译成android时.须要改动jni/android.mk,每一个cpp都改动一下太麻烦,能够让他自己主动识别. # 遍历文件夹及子文件夹的函数 define walk $(wildcard $ ...
- 歌词字幕转换制作专家转换LRC-UTF,出错问题,乱码问题,格式问题
我使用歌词字幕转换制作专家把LRC字幕格式转换成UTF格式后竟然是乱码,求助怎么解决. 编码问题... 转换之前,要先把它处理成ANSI码.先用记事本打开lrc,然后文件-> 另存为,在对话框下 ...
- bug list
机型: Samsung Galaxy S GT-I9000 版本: 2.2.1bug: Couldn't create directory for SharedPreferences file xxx ...
- .NET设计模式(4):建造者模式(Builder Pattern)(转载)
概述 在 软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧 烈的变化,但是将它们组合在一起的算法确相对稳 ...
- Vim下的插件管理工具pathogen简介
1.pathogen简介: 通常情况下安装vim插件是将所有的插件和相关的doc文件都安装在一个文件夹中,如$VIM/vim74/plugin目录下,文档在$VIM/vim74/doc目录下,但 ...
- RAID卡 BBU Learn Cycle周期的影响
背景 最近遇到有些带MegaSAS RAID卡的服务器,在业务高峰时突然IO负载飚升得很高,IO性能急剧下降,查了日志及各种设置最后才发现是RAID卡的Cache写策略由 WriteBack变成Wri ...
- ny12 喷水装置(二)
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...