What is the correct way to get layout inflater in Android?

前端 未结 4 1403
终归单人心
终归单人心 2021-01-30 21:47

There is a way to get layoutInflater:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 22:07

    Actually I think that the getLayoutInflater() - Method of Activity is a convenience - method.

    Remember that Activity subclasses Context, so all of the Methods available within Context are also available in the Activity Class.

    Internally there will be a call to LayoutInflater.fromContext() or context.getSystemService(), so I would stick to context.getSystemService both to avoid the unnecessary method call as well to clarify that I am making a call to a system service.

提交回复
热议问题