Android学习笔记之View(一):LayoutInflater
使用LayoutInflater加载布局的两种方式:
第一种:
LayoutInflater
inflater=LayoutInflater.from(context);
inflater.inflate(R.layout.activity_main,null);
这也是最常用的一种。
第二种:
LayoutInflater
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.activity_main,null);
实际上,第一种方式是吧第二种方式个封装起来了。
不管使用那个inflate()方法,最终都会转辗到LayoutInflater的如下方法的
|
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { final AttributeSet attrs = Xml.asAttributeSet(parser); mConstructorArgs[0] = mContext; View result = root; try { int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!"); } final String name = parser.getName(); if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException("merge can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, attrs); } else { View temp = createViewFromTag(name, attrs); ViewGroup.LayoutParams params = null; if (root != null) { params = root.generateLayoutParams(attrs); if (!attachToRoot) { temp.setLayoutParams(params); } } rInflate(parser, temp, attrs); if (root != null && attachToRoot) { root.addView(temp, params); } if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { InflateException ex = new InflateException(e.getMessage()); ex.initCause(e); throw ex; } catch (IOException e) { InflateException ex = new InflateException( parser.getPositionDescription() + ": " + e.getMessage()); ex.initCause(e); throw ex; } return result; } }
|
LayoutInflater主要是通过Android的pull解析方式来解析布局文件的。
那么接下来我们再定义一个布局文件,给它取名为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中。运行结果如下:

当我们修改layout_width和layout_height属性后(比如改为300dp,300dp),再次运行,发现button大小并没有发生改变,是布局文件失效了吗?其实这里,无论怎么修改这两个属性都是没有的。因为layout_width和layout_height都是用来设置view在布局中的大小。
再来看一个布局
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#ff0000"
android:id="@+id/mainLayout">
</RelativeLayout>
在onCreate方法中执行如下操作:
Protected void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
运行结果如下

为什么这里的layout_width和layout_height属性又起作用了,那是因为在setContentView()方法中,Android会自动在布局文件最外层加一个FrameLayout,所以这里的layout_width和layout_height属性才有用。
那么我们来证实一下吧。修改onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
ViewParent viewParent = mainLayout.getParent();
Log.d("TAG", "the parent of mainLayout is " + viewParent);
}
运行结果如下

Android学习笔记之View(一):LayoutInflater的更多相关文章
- Android学习笔记之View(二)
View加载的流程之测量:rootView调用measure()→onMeasure(): measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以on ...
- [Android学习笔记]获取view的尺寸和坐标
对于UI方面很多时候需要获取它的很多信息,具体情况见view的文档 View文档 http://developer.android.com/training/index.html 常用方法:获取vie ...
- Android学习笔记之View
转载: 0.7562018.10.22 21:44:10字数 5,423阅读 189 导图 一.View事件体系 1.什么是 View 和 View的位置坐标 View是什么: View 是一种界 ...
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- 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应用开发提供了一种 ...
随机推荐
- 移动开发语言Swift
苹果公布了全新的编程语言Swift,Swift继承了Objective-C语言特性,并从Python和Java Script中长处,使Swift更易读.未来swift编程语言的会特大广大的使用 Swi ...
- URAL 1203 Scientific Conference dp?贪心
题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...
- Notepad++中Windows,Unix,Mac三种格式
Notepad++中Windows,Unix,Mac三种格式之间的转换 http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htm ...
- IOS开发:UIAlertView使用
链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...
- css基本框架
- vagrant 入门3
创建第一个Vagrant虚拟环境以及工程(续集): (8) Provisioning: 通常情况下Box只做最基本的设置,而不是一次到位的设置好所有的环境.Vagrant通常使用chef或者Puppe ...
- logback自定义格式转换器
创建自定义格式转换符有两步. 首先,必须继承ClassicConverter类.ClassicConverter对象负责从ILoggingEvent 提取信息,并产生一个字符串.例如,LoggerCo ...
- 异步和同步http请求超时机制
异步超时设置: 例子: Example: do a simple HTTP GET request for http://www.nethype.de/ and print the response ...
- 「操作系统」:Linker Use static Libraries
While static libraries are useful and essential tools, they are also a source of confusion to progra ...
- poj 1035 Spell checker(hash)
题目链接:http://poj.org/problem?id=1035 思路分析: 1.使用哈希表存储字典 2.对待查找的word在字典中查找,查找成功输出查找成功信息 3.若查找不成功,对word增 ...