android layoutinflater 高度,android layoutinflater 参数

LayoutInflater.from(Context context),这里调用时传入getApplicationContext()和某个Activity.this时效果不同。

public static LayoutInflater from(Context context) {

LayoutInflater LayoutInflater =

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (LayoutInflater == null) {

throw new AssertionError("LayoutInflater not found.");

}

return LayoutInflater;

}

它调用了Context.getSystemService这个方法,然而该方法在activity的父类ContextThemeWrapper中被重写了:

@Override public Object getSystemService(String name) {

if (LAYOUT_INFLATER_SERVICE.equals(name)) {

if (mInflater == null) {

mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);

}

return mInflater;

}

return getBaseContext().getSystemService(name);

}

因而传入Activity对象作context参数时,还能获取该activity的主题等属性,从而使得LayoutInflater的inflate方法返回的View可能有所不同。

另外inflate方法的root参数,不为null时,意思是设定root为该布局的父控件/布局,然后即可根据父布局计算该布局长宽等属性;attachToRoot默认为真,作用为添加当前布局到父布局,相当于省去父布局addView()这步而已;为false时,不自动添加到父布局。为null时,无视attachToRoot,采用默认计算方法计算该布局。

注意:用ListView等时,Adapter的getView中root需要填null,因为setAdapter()方法会自动把view挂到ListVIew下。即不填null将可能导致重复挂到ListView上而出差。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值