自定义布局DialogFragment OnCreateView与OnCreateDialog(Cu

2019-06-18 22:40发布

我试图创建一个使用我自己的布局一个DialogFragment。

我见过一对夫妇不同的方法。 有时布局在OnCreateDialog设置是这样的:(我使用的是单声道,但我已经得到了一定程度上使用到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

我难倒。

Answer 1:

第一种方法对我的作品...直到我想使用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();


Answer 2:

我曾与下面的代码相同的异常:

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;    
    }
}

你必须选择覆盖 onCreateView或onCreateDialog 之一的DialogFragment。 重写都将导致异常:“requestFeature()必须在添加内容之前被称为”。

重要

对于完整的答案检查@TravisChristian评论。 正如他所说的,你确实可以同时覆盖,但是当你试图在已经建立的对话视图后膨胀的观点问题就来了。



Answer 3:

下面的代码来自谷歌引导,所以答案是,你不能在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;
    }
}


Answer 4:

下面是对话片段使用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();


    }


Answer 5:

作为@Xavier EGEA说,如果你有两个onCreateView()和onCreateDialog()来实现,运行得到的风险撞车“requestFeature()必须在添加内容之前被称为”。 这是因为这两个onCreateDialog(),那么当你告诉()的片段作为一个对话框(为什么,我不知道)onCreateView()被调用。 由于特拉维斯基督教所提到的,一个对话框后充气()在onCreateView()在onCreateDialog(创建)是什么原因导致飞机坠毁。

一种方法来实现这两种功能,但要避免这种碰撞:使用getShowsDialog()来限制你的onCreateView的执行()(所以你的充气()不叫)。 只有这样,当你显示你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;
}


Answer 6:

如果你想很容易获得的对话框属性,如标题和解雇按钮,但你也想用自己的布局,你可以使用一个LayoutInflator与制作工具时,重写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();
}


文章来源: Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog