gpt4 book ai didi

java - DialogFragment OnCreateView 与 OnCreateDialog 的自定义布局

转载 作者:IT老高 更新时间:2023-10-28 11:51:56 26 4
gpt4 key购买 nike

我正在尝试使用我自己的布局创建一个 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

我被难住了。

最佳答案

以下代码有同样的异常:

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 评论。正如他所说,您确实可以同时覆盖两者,但是当您在创建对话框 View 后尝试膨胀 View 时就会出现问题。

关于java - DialogFragment OnCreateView 与 OnCreateDialog 的自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com