setContentView和inflate区别
一般用LayoutInflater做一件事:inflate
inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。
其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。
int resource,也就是resource/layout文件在R文件中对应的ID,这个必须指定。
而ViewGroup root则可以是null,null时就只创建一个resource对应的View,不是null时,会将创建的view自动加为root的child。
setContentView和inflate区别:
setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来
一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载
< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
***********************************************************
接下来结合源码说说inflate方法的四种形式:
inflate方法总共有四种形式,把xml表达的layout转化为view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on.
1. Context.public abstract object getSystemService(String name)
2. 两种获得LayoutInflater的方法
a. 通过SystemService获得
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
b. 从给定的context中获取
Public static LayoutInflater from(Context context)
c. 两者的区别:实际上是一样的,源码
/**
* Obtains the LayoutInflater from the given 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;
}
3. LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。
4. LinearLayout linearLayout =
(LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text);
5. findViewById有两种形式
R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常
a. activity中的findViewById(int id)
b. View 中的findViewById(int id)
6.不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)
setContentView和inflate区别的更多相关文章
- Android中LayoutInflater的使用
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layou ...
- LayoutInflater
Android中LayoutInflater的使用 博客分类: Android Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 f ...
- Android 开发中关于layoutinflater
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout ...
- 我的Android进阶之旅------>经典的大牛博客推荐(排名不分先后)!!
本文来自:http://blog.csdn.net/ouyang_peng/article/details/11358405 今天看到一篇文章,收藏了很多大牛的博客,在这里分享一下 谦虚的天下 柳志超 ...
- 转载《 LayoutInflater 的inflate函数用法详解》
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...
- LayoutInflater的inflate函数用法详解
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
- LayoutInflater的inflate函数用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
- 转载 LayoutInflater的inflate函数用法详解
http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...
- [转]LayoutInflater的inflate函数用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
随机推荐
- Ubuntu下vi编辑器不听话
编辑文件/etc/vim/vimrc.tiny,将“compatible”改成“nocompatible”非兼容模式: 并添加一句:set backspace=2
- C# 中使用using的三种方法
1.using指令 using+命名空间,这种方法基本学习过C#的都用过,好处在于,写代码的时候不需要指定详细的命名空间 using System.Windows.Media; using Syste ...
- 基于任务的异步编程模式(TAP)
异步编程是C#5.0的一个重要改进,提供两个关键字:async和await.使用异步编程,方法的调用是在后台运行(通常在线程或任务的帮助下),但不会阻塞调用线程.异步模式分为3种:异步模式.基于事件的 ...
- 团队内的沟通方式:网络 OR 当面
4月1日,我和老王稍微聊了聊关于互联网的工作方式,团队的成员不在一个地方,通过远程互联网的方式进行沟通是不是OK.现在确实有很多公司是有多地的分公司,子公司,不同的团队在不同的办公室中,甚至一个大团队 ...
- kali linux之主动信息收集(二层发现)
主动信息收集: 直接与目标系统交互信息,无法避免留下访问的痕迹 使用受控的第三方电脑进行探测,如(使用代理或者使用肉鸡,做好被封杀的准备,使用噪声迷惑目标,淹没真实的探测流量) 识别活着的主机,会有潜 ...
- DISCUZ 各数据库表作用
链接原文:http://forum.digitser.cn/forum.php?mod=viewthread&tid=179 DISCUZ数据字典 http://w ...
- C# Winform下一个热插拔的MIS/MRP/ERP框架(多语言方案)
个别时候,我们需要一种多语种切换方案. 我的方案是这样的: 1.使用文本文本存储多语言元素,应用程序启动时加载到内存表中: 2.应用程序启动时从配置文件加载语种定义: 3.所有窗体继承自一个Base基 ...
- nginx公网IP无法访问浏览器
配置服务器时候发现的问题,真的是搜肠刮肚的找答案,找一下午,终于找到了答案. 一.开始找原因 在浏览器输入:http://ip,正常的话,会有页面,welcome to nginx 我这里是浏览器访问 ...
- The server of Apache (一)——apache服务的基本安装过程
一.为了避免端口冲突,需要卸载linux系统中以RPM方式安装的httpd ~] # rpm -qa | grep httpd ~] # rpm -e httpd --nodeps (此处nodeps ...
- 3-19bug随即
#方案录入 ### 国外网络访问,录入添加图片,上传后图片显示不出. ``` * 后台有返回数据,前端显示巨慢,待检查 * ``` ### 产品信息,富文本信息加载不出