我正在尝试使用自己的布局创建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
我很难过。
答案 0 :(得分:53)
我在以下代码中遇到了同样的异常:
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()”。
要获得完整答案,请查看@TravisChristian评论。正如他所说,你可以确实覆盖它们,但是当你在创建对话框视图后尝试给视图充气时会出现问题。
答案 1 :(得分:35)
这第一种方法对我有用......直到我想使用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();
答案 2 :(得分:30)
下面的代码来自google guide,所以答案是你在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;
}
}
答案 3 :(得分:16)
以下是在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();
}
答案 4 :(得分:10)
正如@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;
}
答案 5 :(得分:5)
如果您希望轻松访问对话框属性,例如标题和关闭按钮,但您还想使用自己的布局,则可以在覆盖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();
}