自定义布局DialogFragment OnCreateView与OnCreateDialog [英] Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog

查看:1605
本文介绍了自定义布局DialogFragment OnCreateView与OnCreateDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我见过一对夫妇不同的方法。有时,布局设置在OnCreateDialog是这样的: (我使用的是单声道,但我已经得到了一定程度用于Java的)

 公众覆盖Android.App.Dialog OnCreateDialog(包savedInstanceState)
{
    base.OnCreateDialog(savedInstanceState);
    AlertDialog.Builder B =新AlertDialog.Builder(活动);
        //等等等等等等
    LayoutInflater I = Activity.LayoutInflater;
    b.SetView(i.Inflate(Resource.Layout.frag_SelectCase,NULL));
    返回b.Create();
}
 

这第一种方式对我的作品......直到我想使用 findViewByID。 打完有点谷歌上搜索我的尝试涉及重写第二种方法 OnCreateView

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

 公众覆盖Android.Views.View OnCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
    视图V = inflater.Inflate(Resource.Layout.frag_SelectCase,集装箱,假);
        //应该可以在这里使用FindViewByID ...
    返回伏;
}
 

这给了我一个美丽的错误:

  11-05 22:00:05.381:E / AndroidRuntime(342):致命异常:主要
11-05 22:00:05.381:E / AndroidRuntime(342):android.util.AndroidRuntimeException:requestFeature()必须添加内容之前调用
 

我难倒。

解决方案
  

这第一种方式对我的作品......直到我想使用FindViewByID。

我猜你是不是作用域 findViewById()来)由返回查看膨胀(,尝试这样的:

 查看视图= i.inflate(Resource.Layout.frag_SelectCase,NULL);
//现在使用view.findViewById()做你想要什么
b.setView(视图);

返回b.create();
 

I'm trying to create a DialogFragment using my own Layout.

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

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

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

I'm stumped.

解决方案

This first approach works for me... until I want to use FindViewByID.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆