android的inflate方法

首先,有View。inflate()和Layoutinflater。inflate()两个方法,但看一下源码就知道其实是一种:

 public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
    }

再者,获取LayoutInflater对象的方法,不止上面写的LayoutInflater.from(context)一种,另外还有两种,现在总结一下:

LayoutInflater factory=LayoutInflater.from(this);
  
 LayoutInflater factory=getLayoutInflater();
  
 LayoutInflater factory=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

-->现在查看一下每一种方法的内部实现源码:

-->第一种方法的源码:

/**  LayoutInflater.class
     * Obtains the LayoutInflater from the given context.
     */
    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;
    }

-->评析:LayoutInflater.from(context)方法调用的是“第三种方法”:context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

-->第二种方法源码:

/**  Activity.class
     * Convenience for calling
     * {@link android.view.Window#getLayoutInflater}.
     */
    public LayoutInflater getLayoutInflater() {
        return getWindow().getLayoutInflater();
    }

-->评析:Activity.getLayoutInflater()方法调用的是Window.getLayoutInflater()方法:

/**  Window.class
     * Quick access to the {@link LayoutInflater} instance that this Window
     * retrieved from its Context.
     *
     * @return LayoutInflater The shared LayoutInflater.
     */
    public abstract LayoutInflater getLayoutInflater();

-->评析:但是Window.getLayoutInflater()方法找不到调用方法了。通过它的方法描述大概可以知道也是调用“第三种方法”:context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

-->第三种方法的源码:

 
 
/**  Activity.class*/
  @Override
    public Object getSystemService(String name) {
        if (getBaseContext() == null) {
            throw new IllegalStateException(
                    "System services not available to Activities before onCreate()");
        }

        if (WINDOW_SERVICE.equals(name)) {
            return mWindowManager;
        } else if (SEARCH_SERVICE.equals(name)) {
            ensureSearchManager();
            return mSearchManager;
        }
        return super.getSystemService(name);
    }

-->评析:this.getSystemService(LAYOUT_INFLATER_SERVICE)方法调用的是其父类的getSystemService(LAYOUT_INFLATER_SERVICE);

public class Activity extends ContextThemeWrapper

-->评析:从上面的代码知道,Activity类的父类是ContextThemeWrapper类。

/** ContextThemeWrapper.class
*/
@Override public Object getSystemService(String name) {
        if (LAYOUT_INFLATER_SERVICE.equals(name)) {
            if (mInflater == null) {
                mInflater = LayoutInflater.from(mBase).cloneInContext(this);
            }
            return mInflater;
        }
        return mBase.getSystemService(name);
    }

-->评析:下面看看mBase是什么:

public class ContextThemeWrapper extends ContextWrapper {
    private Context mBase;
  ...

-->评析:ContextThemeWrapper.getSystemService(LAYOUT_INFLATER_SERVICE)方法调用的是Context.getSystemService(LAYOUT_INFLATER_SERVICE)方法:

/** Context.class
*/ 
public abstract Object getSystemService(String name);

-->评析:追根溯源暂时只能到这了。我也不知道它是怎么实现了,或许在Context的子类中有实现。我已经不想去找了。

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值