What is the difference between calling LayoutInflater directly and not?

前端 未结 2 1692
谎友^
谎友^ 2021-02-14 22:44

I went through some tutorials, and in the Android Doc, it says not to access LayoutInflater directly when instantiating it. Example from the google Doc:

LayoutIn         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 22:59

    If you open up the Android source you can see that the LayoutInflator.from method looks like so:

    /**
     * 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;
    }
    

    That means the two lines of code in your question do the same thing. Not sure what the tutorial you read says exactly but I don't see any difference in functionality. Using the from method is nice since it requires a little less typing, that's it.

提交回复
热议问题