首页 > 试题广场 >

关于LayoutInflater的说法正确的是

[不定项选择题]
关于LayoutInflater的说法正确的是
  • LayoutInflater的from方法是static(静态)的
  • LayoutInflater的inflate方法可以有三个参数
  • 在Activity中可以通过getLayoutInflater方法获得LayoutInflater对象
  • LayoutInflater的具有一个参数Context对象的构造方法
这道题目本身没有多大意义,但是D选项值得琢磨。LayoutInflater是一个抽象类
public abstract class LayoutInflater
抽象类不能直接实例化,那为啥还会有构造方法?其实,抽象类是可以有构造方法的,不要被网上的说法骗了。看这篇博客:
发表于 2019-09-13 16:27:57 回复(0)
我对这题有疑议,C选项好像没问题吧,求大佬指点
发表于 2019-11-04 10:16:16 回复(0)
基于android Q(API 29) 源码来看,题目有误,全部选项都是正确的。
1. 选项A正确
    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;
    }

2. 选项B正确
    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
        if (DEBUG) {
            Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                  + Integer.toHexString(resource) + ")");
        }

        View view = tryInflatePrecompiled(resource, res, root, attachToRoot);
        if (view != null) {
            return view;
        }
        XmlResourceParser parser = res.getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

3.选项C正确
    @NonNull
    public LayoutInflater getLayoutInflater() {
        return getWindow().getLayoutInflater();
    }

4.选项D正确
    protected LayoutInflater(Context context) {
        mContext = context;
        initPrecompiledViews();
    }
发表于 2020-06-28 10:20:26 回复(0)
1.查看LayoutInflater方法,from方法是公有的,静态的
2.inflate方法有三个参数,第一个是fragment的布局,第二个是container,第三个默认为false

3.目前还没看到过这个方法
4.查看Layout源码,源码中确实存在一个参数context的构造方法
发表于 2020-02-10 10:36:34 回复(1)
在activity中通过
getLayoutInflater方法获得LayoutInflater对象 
没有毛病吧?

发表于 2019-11-16 22:21:12 回复(0)
/**  * Convenience for calling  * {@link android.view.Window#getLayoutInflater}.  */ @NonNull public LayoutInflater getLayoutInflater() { return getWindow().getLayoutInflater(); }
C好像没问题吧,我看Activity源码里面确实是返回LayoutInFlater对象
发表于 2020-03-15 11:19:04 回复(0)
这种题目都不审核的吗
发表于 2020-08-25 22:05:50 回复(0)
C用了3年了我都不知道有什么错误呢
发表于 2020-08-03 15:56:32 回复(0)
查一下源码
发表于 2020-03-18 19:32:00 回复(0)
C是在fragment 使用?
getLayoutInflater
发表于 2020-02-04 19:56:32 回复(0)