Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog

后端 未结 6 1618
庸人自扰
庸人自扰 2020-12-01 00:31

I\'m trying to create a DialogFragment using my own Layout.

I\'ve seen a couple different approaches. Sometimes the layout is set in OnCreateDialog like this: (I\'

6条回答
  •  无人及你
    2020-12-01 01:24

    Below code comes from google guide, so the answer is that you could not do like yours in onCreateDialog(), you must use super.onCreateDialog() to get a dialog.

    public class CustomDialogFragment extends DialogFragment {
        /** The system calls this to get the DialogFragment's layout, regardless
            of whether it's being displayed as a dialog or an embedded fragment. */
        @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);
        }
    
        /** The system calls this only when creating the layout in a dialog. */
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // The only reason you might override this method when using onCreateView() is
            // to modify any dialog characteristics. For example, the dialog includes a
            // title by default, but your custom layout might not need it. So here you can
            // remove the dialog title, but you must call the superclass to get the Dialog.
            Dialog dialog = super.onCreateDialog(savedInstanceState);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            return dialog;
        }
    }
    

提交回复
热议问题