DialogFragment OnCreateView 与 OnCreateDialog 的自定义布局 [英] Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog
问题描述
我正在尝试使用我自己的布局创建一个 DialogFragment.
I'm trying to create a DialogFragment using my own Layout.
我见过几种不同的方法.有时布局在 OnCreateDialog 中设置如下:(我使用的是 Mono,但我已经有点习惯 Java)
I've seen a couple different approaches. Sometimes the layout is set in OnCreateDialog like this: (I'm using Mono but I've gotten somewhat used to 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
This first approach works for me... until I want to use findViewByID.
so after a bit of googling I tried the second approach which involves overriding OnCreateView
所以我注释掉了设置布局的两行 OnCreateDialog
然后添加了这个:
So I commented out two lines of OnCreateDialog
that set the Layout and then added this:
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;
}
这给了我一个可爱的错误:
which gives me a lovely error:
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.
This first approach works for me... until I want to use FindViewByID.
我猜你没有将 findViewById()
范围限定为 inflate()
返回的视图,试试这个:
I would guess that you are not scoping findViewById()
to the View returned by inflate()
, try this:
View view = i.inflate(Resource.Layout.frag_SelectCase, null);
// Now use view.findViewById() to do what you want
b.setView(view);
return b.create();
这篇关于DialogFragment OnCreateView 与 OnCreateDialog 的自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!