android LayoutInflater的获取

LayoutInflater 获取有三种方法。

LayoutInflater mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;

LayoutInfalter LayoutInfalter.from(Context contextf);


这两个函数是一样的结果:

LayoutInflater.from 的实现如下:

    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;

也就是调用了上面getSystemService。但是增加了LayoutInflater是否为空的判断。

还有一个Activity的函数 getLayoutInflater() 也能够返回LayoutInflater 但是返回的Window 的getLayoutInflater() (其实就是PhoneWindow 的实例的LayoutInflater)

Activity.java:

mWindow = PolicyManager.makeNewWindow(this);

 /**
     * Convenience for calling
     * {@link android.view.Window#getLayoutInflater}.
     */
    public LayoutInflater getLayoutInflater() {
        return getWindow().getLayoutInflater();
    }
 /**
     * Retrieve the current {@link android.view.Window} for the activity.
     * This can be used to directly access parts of the Window API that
     * are not available through Activity/Screen.
     * 
     * @return Window The current window, or null if the activity is not
     *         visual.
     */
    public Window getWindow() {
        return mWindow;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值