Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog

后端 未结 6 1613
庸人自扰
庸人自扰 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:16

    If you want to have easy access the dialog properties, like the title and the dismiss button, but you also want to use your own layout, you can use a LayoutInflator with your Builder when you override onCreateDialog.

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Message!")
            .setTitle(this.dialogTitle)
            .setView(inflater.inflate(R.layout.numpad_dialog, null))
            .setPositiveButton(R.string.enter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Clicked 'Okay'
                }
            })
            .setNegativeButton(R.string.dismiss, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Clicked 'Cancel'
                }
            });
        return builder.create();
    }
    

提交回复
热议问题