Full screen DialogFragment

前端 未结 10 2002
执笔经年
执笔经年 2021-02-04 02:26

I am trying to create a DialogFragment with a width of MATCH_PARENT so the dialog is nearly full screen (leaving the padding around the edges for the floating look). I have see

10条回答
  •  滥情空心
    2021-02-04 02:54

    To have a full screen dialog, you need use

    public class CustomDialogFragment extends DialogFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout to use as dialog or embedded fragment
            return inflater.inflate(R.layout.purchase_items, container, false);
        }
    
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Dialog dialog = super.onCreateDialog(savedInstanceState);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            return dialog;
        }
    }
    

    And to show this dialog:

    CustomDialogFragment newFragment = new CustomDialogFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    // To make it fullscreen, use the 'content' root view as the container
    // for the fragment, which is always the root view for the activity
    fragmentManager.beginTransaction().add(android.R.id.content, newFragment).commit();
    

    ATTENTION: You can't use following code to show the dialog, if not, the dialog will be shown in center of screen, not full screen.

    //don't show dialog like this
    newFragment.show(fragmentManager, "dialog");
    

提交回复
热议问题