getLayoutInflater() in fragment

后端 未结 5 757
我寻月下人不归
我寻月下人不归 2020-12-04 17:27

I\'m trying to show my results search in a ListView on the Fragment, but it fails on:

LayoutInflater inflater = getLayoutInflater()         


        
相关标签:
5条回答
  • 2020-12-04 18:15

    In Activity you can use like this:

    this.getLayoutInflater();
    

    For Fragment you need to replace keyword "this" with "getActivity()"

    getActivity().getLayoutInflater();
    

    Hope this will help!

    0 讨论(0)
  • 2020-12-04 18:18

    if you are in a fragment you want

    getActivity().getLayoutInflater();
    

    or

    LayoutInflater.from(getActivity());
    

    also you can do

    View.inflate();
    

    or

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    0 讨论(0)
  • 2020-12-04 18:18

    If you want to inflate layout (attach child layout to parent) in Fragment

    Use this code

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

    Now to Inflate child layout

    View view = inflater.inflate(R.layout.layout_child, null);
    

    Happy coding :)

    0 讨论(0)
  • 2020-12-04 18:25

    Use it:

    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    0 讨论(0)
  • 2020-12-04 18:29

    Now and don't know from when you can get it in your Fragment(support library v4) by

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