对LayoutInflater的简单理解

LayoutInflater的获得实例的三种方法

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);  

众所周知,inflater.inflate的三种加载模式

1.inflate(layoutId, null );
2.inflate(layoutId, root, false );
3.inflate(layoutId, root, true )

通过观察下面的源码我们可以知道,1和2是进入了第二个条件即没有附带参数,即你在xml写的参数是不会起作用的,要想参数起作用就只有用第3的一个方法。

// 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;
}

其次对于1,2也是有区别的,1是直接加载出一个组件,2是加载出一个组件后还要将组件放到root布局里面去,最后返回的视图就是组件加入后的视图。所以1,2就必须要动态填入参数,wrap_content或match_parent。1还需要addView()将他加入父布局如下图:
这里写图片描述
最后福利一个非常有用的链接:http://blog.csdn.net/yanbober/article/details/45970721

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值