Call to getLayoutInflater() in places not in activity

别来无恙 提交于 2019-11-27 02:32:50
kaspermoerch

You can use this outside activities - all you need is to provide a Context:

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

Then to retrieve your different widgets, you inflate a layout:

View view = inflater.inflate( R.layout.myNewInflatedLayout, null );
Button myButton = (Button) view.findViewById( R.id.myButton );

EDIT as of July 2014

Davide's answer on how to get the LayoutInflater is actually more correct than mine (which is still valid though).

Or ...

LayoutInflater inflater = LayoutInflater.from(context);

or

View.inflate(context, layout, parent)

Using context object you can get LayoutInflater from following code

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Sikander Bakht
LayoutInflater.from(context).inflate(R.layout.row_payment_gateway_item, null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!