Call to getLayoutInflater() in places not in activity

后端 未结 6 1292
长情又很酷
长情又很酷 2020-11-29 16:18

What does need to be imported or how can I call the Layout inflater in places other than activity?

public static void method(Context context){
    //this doe         


        
相关标签:
6条回答
  • 2020-11-29 16:25

    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).

    0 讨论(0)
  • 2020-11-29 16:25

    Using context object you can get LayoutInflater from following code

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    0 讨论(0)
  • 2020-11-29 16:30

    Or ...

    LayoutInflater inflater = LayoutInflater.from(context);
    
    0 讨论(0)
  • 2020-11-29 16:30
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    

    Use this instead!

    0 讨论(0)
  • 2020-11-29 16:34
    LayoutInflater.from(context).inflate(R.layout.row_payment_gateway_item, null);
    
    0 讨论(0)
  • 2020-11-29 16:47

    or

    View.inflate(context, layout, parent)

    0 讨论(0)
提交回复
热议问题