Android中关于布局填充器的各种详细用法和说明

1.通过系统获取系统的布局管理器,这种方式通常是当我们获取系统的应用或者和系统相关的操作时使用的

 LayoutInflater inflater=(LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);如果在Activity中可以省略Context,如果在一个类中就要使用上下文;

View mView=inflater.inflate(Context,R.layout.XX,root);

2.在Activity中要给某个ViewGroup添加子布局时使用以下方式

View  mView=getLayoutInflate.inflate(R.layout.xx,root,false);

3.通过View的静态方式获取,这种通常用在自定义控件中,比如自定义dialog或者toast中都可以使用

View mView=View.inflate(Context,R.layout.xx,false);

4,通过LayoutInflate的inflate()方法来是实现,典型的BaseAdapter中加载布局的方式

View view=LayoutInflater.from(Context).inflate(R.layout.xx,rootView,false);

以上就是Layoutinflate的四种用法。下面简单的通过代码来说一下这四种之间的关系或者本质的区别吧

第一:在Activity中,

public  Layoutinflater getLayoutInflater(){

return getWindow().getLayoutInflater();}

 而Activity在底层还是调用了抽象类Window的方法:

 public LayouInflater getLayoutInflater(Context context){

return  LayoutInflate.from(Context context);

},而window类中是如何调用的呢?请看下面内容

Window是通过静态的方式获取LayoutInflater的

 public   static  View  inflate(Context mContext, LayoutRes  res, ViewGroup root){

LayoutInflater  mLayoutInflater=LayoutInflater.from(mContext);

return mLayoutInflater.inflate(res,root);

}

值得注意的是LayoutInflater也是一个abstract类,它最终还是调用系统的方式获取的

public abstract  LayoutInflater  extends Object{

public static  static  LayoutInflater from(Context mContext){

 LayoutInflater mLayoutInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(mLayoutInflater==null){

throws  Exception(Exception e);

}

return  mLayoutInflater;





}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值