DialogFragment OnCreateView与OnCreateDialog的自定义布局

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DialogFragment OnCreateView与OnCreateDialog的自定义布局相关的知识,希望对你有一定的参考价值。

我正在尝试使用自己的布局创建一个DialogFragment。

我见过几种不同的方法。有时布局是在OnCreateDialog中设置的,如下所示:(我正在使用Mono,但我已经习惯了Java)

public override android.App.Dialog OnCreateDialog (Bundle savedInstanceState)
{
    base.OnCreateDialog(savedInstanceState);
    AlertDialog.Builder b = new AlertDialog.Builder(Activity);
        //blah blah blah
    LayoutInflater i = Activity.LayoutInflater;
    b.SetView(i.Inflate(Resource.Layout.frag_SelectCase, null));
    return b.Create();
}

这第一种方法适用于我...直到我想使用findViewByID.所以经过一些谷歌搜索我尝试了第二种方法涉及覆盖OnCreateView

所以我注释掉两行OnCreateDialog设置布局,然后添加:

public override Android.Views.View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.Inflate(Resource.Layout.frag_SelectCase, container, false);
        //should be able to use FindViewByID here...
    return v;
}

这给了我一个可爱的错误:

11-05 22:00:05.381: E/AndroidRuntime(342): FATAL EXCEPTION: main
11-05 22:00:05.381: E/AndroidRuntime(342): android.util.AndroidRuntimeException: requestFeature() must be called before adding content

我很难过。

答案

第一种方法适用于我...直到我想使用FindViewByID。

我猜你不会将findViewById()放到inflate()返回的视图中,试试这个:

View view = i.inflate(Resource.Layout.frag_SelectCase, null);
// Now use view.findViewById() to do what you want
b.setView(view);

return b.create();
另一答案

我有以下代码的相同异常:

public class SelectWeekDayFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
        .setMessage("Are you sure?").setPositiveButton("Ok", null)
        .setNegativeButton("No way", null).create();
    }

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

        return view;    
    }
}

您必须选择仅覆盖DialogFragment中的onCreateView或onCreateDialog中的一个。覆盖两者将导致异常:“在添加内容之前必须调用requestFeature()”。

Important

如需完整答案,请查看@TravisChristian评论。正如他所说的那样,你可以确实覆盖它们,但是当你在创建对话框视图后尝试给视图充气时会出现问题。

另一答案

下面的代码来自谷歌指南,所以答案是你在onCreateDialog()中不能像你的那样,你必须使用super.onCreateDialog()来获得一个对话框。

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;
    }
}
另一答案

以下是在Dialog Fragment中使用findViewById的示例

public class NotesDialog extends DialogFragment {

        private ListView mNotes;
       private RelativeLayout addNote;

        public NotesDialog() {
            // Empty constructor required for DialogFragment
        }



        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            View view = getActivity().getLayoutInflater().inflate(R.layout.note_dialog, null);
            mNotes = (ListView) view.findViewById(R.id.listViewNotes);
            addNote = (RelativeLayout) view.findViewById(R.id.notesAdd);

            addNote.setOnClickListener(new View.OnClickListener(){
                 @Override
                 public void onClick(View v){


                     getDialog().dismiss();

                     showNoteDialog();
                 }
             });

            builder.setView(view);

            builder.setTitle(bandString);


            builder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                          getDialog().dismiss();
                        }
                    }
                );


           return  builder.create();


    }
另一答案

正如@Xavier Egea所说,如果你同时实现onCreateView()和onCreateDialog(),你就有可能在添加内容之前获得“requestFeature()必须被调用”崩溃。这是因为当你将那个片段作为一个对话框显示(为什么,我不知道)时,会调用onCreateDialog()然后调用onCreateView()。正如Travis Christian所提到的,在onCreateDialog()中创建对话框之后onCreateView()中的inflate()是导致崩溃的原因。

实现这两个函数的一种方法,但避免这种崩溃:使用getShowsDialog()来限制onCreateView()的执行(因此不调用inflate())。这样,当您将DialogFragment显示为对话框时,只会执行onCreateDialog()代码,但是当您将DialogFragment用作布局中的片段时,可以调用onCreateView()代码。

// Note: if already have onCreateDialog() and you only ever use this fragment as a 
// dialog, onCreateView() isn't necessary
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (getShowsDialog() == true) {  // **The key check**
        return super.onCreateView(inflater, container, savedInstanceState);
    } else {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_alarm_dialog, null);    
        return configureDialogView(view);
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{ 
    // Return custom dialog...
    Dialog dialog = super.onCreateDialog(savedInstanceState); // "new Dialog()" will cause crash

    View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_alarm_dialog, null);    
    configureDialogView(view);
    dialog.setContentView(view);

    return dialog;
}

// Code that can be reused in both onCreateDialog() and onCreateView()
private View configureDialogView(View v) {      
    TextView myText = (TextView)v.findViewById(R.id.myTextView);
    myText.setText("Some Text");

    // etc....

    return v;
}
另一答案

如果您希望轻松访问对话框属性(如标题和关闭按钮),但您还想使用自己的布局,则可以在覆盖onCreateDialog时将LayoutInflator与Builder一起使用。

@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();
}

以上是关于DialogFragment OnCreateView与OnCreateDialog的自定义布局的主要内容,如果未能解决你的问题,请参考以下文章

Dialogfragment 未清除

Android:DialogFragment.dismissInternal 处 DialogFragment.dismissAllow 处的 NullPointerException

DialogFragment.getDialog 返回 null

为啥要使用 DialogFragment?

从 DialogFragment 回调片段

允许 DialogFragment 的外部触摸