Android fragment show as dialogfragment or usual fragment

前端 未结 3 1483
夕颜
夕颜 2020-12-30 06:10

What I am trying to achieve is to have a fragment that on tablet it shows as a DialogFragment, while on smartphone it would be shown as a regular fragment. I am

3条回答
  •  生来不讨喜
    2020-12-30 06:39

    To make a DialogFragment show as a regular Fragment, call add() or replace() using a resource ID for the fragment container, e.g

    beginTransaction().add(R.id.fragment_container, fragment)
    

    But to make the DialogFragment display as a dialog, call add(fragment, "Fragment Tag"). Behind the scenes this results in a call to add(resId, fragment), setting the resource ID for the fragment container to 0, which causes the DialogFragment to set its showAsDialog option to true.

    As a consequence you can use a dialog fragment as a dialog or a regular fragment - depending on your needs - without needing to create any special logic to do it.

提交回复
热议问题