0

Screenshot

This image came from the official documents.But I don't understand one of the words:"or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)"

Can you explain it for me?

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
wpumain
  • 55
  • 5

1 Answers1

0

Inflate is used to add a view, especially used for inflating fragments inside an activity. ViewGroup root is the parent view in which you want to inflate your view. If you are using this function to inflate a fragment then use it like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_name,container,false);
    return view;
} 

If this does not answer your query then let me know in what context are you exactly using inflate.

Deepak Sharma
  • 417
  • 3
  • 9
  • When the second parameter is not null and the third parameter is false,what effect does the second parameter have? – wpumain Mar 21 '16 at 07:47
  • Third parameter defines whether android should inflate the layout(i.e. first parameter) in the container(i.e. second parameter) right after inflate is called or not. It is best to keep the third parameter false irrespective of the container being null or not, and set the fragment using `FragmentManager.beginTransaction()` later on in your activity. – Deepak Sharma Mar 21 '16 at 08:02
  • Thank you for your explanation.I think a related qusetion.When the second parameter is not null and the third parameter is false,how is the LayoutParams of the view inflated by the first parameters seted? – wpumain Mar 22 '16 at 12:02
  • `view.setLayoutParams()` should work in this case. Refer this if it helps http://stackoverflow.com/questions/12224433/can-i-set-layoutparams-to-fragment-by-programming – Deepak Sharma Mar 22 '16 at 12:18
  • Sorry,I still can not understand. – wpumain Mar 22 '16 at 16:18