使用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,代码如下所示:

  1. <Button xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:text="Button" >  
  5.   
  6. </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的更多相关文章

  1. Android学习笔记之View(二)

    View加载的流程之测量:rootView调用measure()→onMeasure(): measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以on ...

  2. [Android学习笔记]获取view的尺寸和坐标

    对于UI方面很多时候需要获取它的很多信息,具体情况见view的文档 View文档 http://developer.android.com/training/index.html 常用方法:获取vie ...

  3. Android学习笔记之View

    转载: 0.7562018.10.22 21:44:10字数 5,423阅读 189   导图 一.View事件体系 1.什么是 View 和 View的位置坐标 View是什么: View 是一种界 ...

  4. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  5. 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter

    目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...

  6. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  7. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  8. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  9. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

随机推荐

  1. Linux下安装VNC Server

    操作系统centos6.5,在其之上安装vnc server,可利用windows上的vnc client远程登录. 1. 安装 yum install tigervnc-server.x86_64 ...

  2. MaxSubArray 最大子数列和

    public int maxSubArray(int[] A) { int newsum=A[0]; int max=A[0]; for(int i=1;i<A.length;i++){ new ...

  3. hdoj 3062 Party(2-SAT)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 思路分析:将问题转换为2-SAT问题,需要注意的是将命题转换为有向图的方法:命题中A1, A2, ...

  4. poj 1836 Alignment(线性dp)

    题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...

  5. How to setup linked servers for SQL Server and Oracle 64 bit client

    感谢作者:Tim Ford. 图文并茂. 原帖地址: http://www.mssqltips.com/sqlservertip/1433/how-to-setup-linked-servers-fo ...

  6. 使用PowerShell 命令集进行SQL Server 2012 备份和还原

    最近心相不错,所以打算翻译一些英文文档做福利,原文在此,翻译有不足的地方还请各位兄弟指点. 讨论什么是DBA最重要的工作的时候,你最常听到就是一条就是DBA只要做好备份和恢复.事实如此,如果你不做备份 ...

  7. NSOperationQueue和GCD的区别

    使用NSOperationQueue用来管理子类化的NSOperation对象,控制其线程并发数目.GCD和NSOperation都可以实现对线程的管理,区别是 NSOperation和NSOpera ...

  8. 自定义cell相关注意事项

    1.拖线成功后,如果又在.h文件或者.m文件里面删除了对应的属性或者方法.一定要在xib文件中,删除关联.方法是:右键点击一下对应的UI控件,把多余的关联叉掉就行了.  不然容易崩溃.

  9. C# Best Practices - Creating Good Methods

    How to Define a Method Identify the problem => Define the single purpose => Specify the inputs ...

  10. URAL DP第一发

    列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...