LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

LayoutInflater的inflate方法一共同拥有四种,但我们日经常使用经经常使用到的就仅仅有这两种:

    public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
    public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
if (DEBUG) System.out.println("INFLATING from resource: " + resource);
XmlResourceParser parser = getContext().getResources().getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}

所以,这里直接介绍里面调用这种方法就可以:

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

在这种方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:

                    ViewGroup.LayoutParams params = null;

                    if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}

params = root.generateLayoutParams(attrs);

这段的意思是:假设调用inflate方法,传入了ViewGroup root參数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们载入的视图View设置该LayoutParams。

接着往下看:

                    // We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
} // Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}

root.addView(temp, params);

假设设置了ViewGroup root參数,且attachToRoot设置为true的话,则将我们载入的视图做为子视图加入到root视图中。

假设我们ViewGroup root设置为空的话,就直接返回我们创建的视图;假设root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们载入的视图View设置该LayoutParams。

以上就是该方法内容,可能你还有点看不太懂吧,下一篇文章,我将会做几个样例,来详细说明一下。

Android编程之LayoutInflater的inflate方法具体解释的更多相关文章

  1. Android编程之LayoutInflater的inflate方法实例

    假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...

  2. Android编程之LayoutInflater的inflate方法详解

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...

  3. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  4. 带你看懂LayoutInflater中inflate方法

    关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...

  5. android LayoutInflater和inflate()方法的用法(转载)

    原文出处:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html 在实际开发中LayoutInflater这个类还是非常有用的,它的作用 ...

  6. 【Android 界面效果43】Android LayoutInflater的inflate方法中attachToRoot的作用

    我们在ListView的Adapter的getView方法里面经常会调用两个参数的inflate方法, mInflater.inflate(R.layout.adv_viewpager, null); ...

  7. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

  8. 【转】三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  9. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

随机推荐

  1. linux swap 分区调控(swap分区 lvm管理)

    注:linux swap分区 采用lvm管理,调控可以采用下面的方法 一.查看 swap    lv [root@testdb ~]# vgdisplay -v Finding all volume ...

  2. 跨越多台haproxy 跳转

    <pre name="code" class="python">在zjtest5环境的haproxy上配置: 需要 访问acl host_zjcap ...

  3. 为什么不能在scrollview中直接添加一个image,然后使animation.begin()??

    http://stackoverflow.com/questions/17267451/animation-cant-begin-in-scrollview-in-windows-phone 以上是我 ...

  4. 动态可视化库Vis.js:社交关系谱

    Form Here:http://code.csdn.net/news/2819345 Vis.js 是一个动态的.基于浏览器的可视化库,可处理大量的动态数据并能与这些数据进行交互操作.该项目是由Al ...

  5. 【Java】使用Runtime执行其他程序

    public class ExecDemo{ public static void main(String[] args) { Runtime r = Runtime.getRuntime(); Pr ...

  6. react-native 自己搭建热更新服务器

    使用到的框架是 react-native-update react-native-update-cli 这个应该执行热更新的时候的终端命令. 通过这个,自己搭建一个热更新的服务器.

  7. eclipse ctrl链接设置

    选择[Window]菜单 Preferences ——>General——>Editors——>Text Editors——>Hyperlinking

  8. _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  9. About Health Monitor Checks

    About Health Monitor Checks Health Monitor checks (also known as checkers, health checks, or checks) ...

  10. SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)

    1.引言 T-SQL(Transact Structured Query Language)是标准的SQL的扩展,是程序和SQL Server沟通的主要语言. T-SQL语言主要由以下几部分组成: 数 ...