LayoutInflater 全方位描述

来源:互联网 发布:mac办公软件收费吗 编辑:程序博客网 时间:2024/06/05 14:46

闲话不说,直奔主题

LayoutInflater的作用,简单来说“加载布局”。

获取LayoutInflater对象,方法有二。

//第一种LayoutInflater inflate = LayoutInflater.from(context);//第二种LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

实质上,当你查看LayoutInflater.from(context)的源代码时,你会发现这两种方法tmd不是一样么? 

恭喜你,答对了。tmd就是一样的。但推荐使用第一用,简单易用,还有多了一个检查判断。(具体看源代码)


使用LayoutInflater对象。如何加载布局呢?就要用它的inflate成员。这样的成员有4个:

public View inflate(int resource, ViewGroup root) ;public View inflate(int resource, ViewGroup root, boolean attachToRoot);
//下面两个方法提供给LayoutInflater内部调用,正常的用户是不会去使用的
public View inflate(XmlPullParser parser, ViewGroup root);
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot);

需要注意的是前两个inflate的成员方法。

public View inflate(int resource, ViewGroup root, booleaadfsn attachToRoot);

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

root:需要附加到resource资源文件的根控件,如果root非空,通过resource加载的view的layoutParams有效,否则无效。并且第三个参数attachToRoot为true,就将这个root作为根对象返回,否则使用resource对象自身定义的LayoutParmas.

attachToRoot:true返回root,否则返回附加到布局文件的根视图上view

把第一个方法的源码贴出来,自己分析,看和第二个inflate方法有什么不一样。

public View inflate(int resource, ViewGroup root) {        return inflate(resource, root, root != null);}


值得注意的是,事实上我们最常用的是inflate(int resource, ViewGroup root);

最常用法是: layoutInflater.inflate(id, null); 这相当于layoutInflate.inflate(id, null, false);也意味着与id对应的xml文件中layout_width与layout_height失效了(这种情况下,iflate的内部代码在创建View时,没有去设置它,所以失效了)。这种情况下,在xml中定不定义这两种属性毫无意义(但其他情况必须定义否则程序出错,不信自己试一下)。所以这点需要注意。看下面程序片段帮助理解。

View v = layoutInflater.inflate(id, null);//得到的v,但其layoutParams为null//没有layoutParams的话,v怎么放入布局中?别担心,addView方法自动判断该属性是否为null,如果为null,则自动添加默认的(wrap_content)。如果不喜欢默认的,那你就使用addView(View child, ViewGroup.LayoutParams params)吧。parent.addView(v);


LayoutInflater 从创建到加载布局所用的方法都讲解了。下面给出Demo。


main_activity.java代码

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);layout = (RelativeLayout)findViewById(R.id.layout);LayoutInflater layoutInflater = LayoutInflater.from(this);layoutInflater.inflate(R.layout.button, layout, true);}

main.xml


<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"    tools:context=".MainActivity"     android:id = "@+id/layout">    </RelativeLayout>


button.xml代码

<?xml version="1.0" encoding="utf-8"?><Button xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:text = "@string/button"></Button>



最后极力推荐大家看这篇博文http://blog.csdn.net/guolin_blog/article/details/12921889,讲得非常清晰到位,但本人虽然水平有限,但写出来的文章还是有一定参照价值的,有写的东西是上述博文没有提到的,请大家结合起来一起看,理解更透彻。

0 0