自定义AlertDialog - 有问题的android.R.id.custom [英] Custom AlertDialog - problem with android.R.id.custom

查看:452
本文介绍了自定义AlertDialog - 有问题的android.R.id.custom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,我错过了点这里,所以我希望有人能解释清楚。

I'm sure I'm missing the point here so I'm hoping someone can explain.

我想,当用户触摸的ImageView创建一个弹出窗口。我在AlertDialog了外观和文档说...

I want to create a popup when a user touches an ImageView. I had a look at AlertDialog and the docs say...

如果你想显示一个更复杂的视图,查找名为定制的FrameLayout,并添加您的看法吧:

If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:

...用下面的code ...

...with the following code...

    FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
    fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

因此​​,作为一个测试,我试图在我的onclick以下()方法...

So as a test I tried the following in my onCLick() method...

    TextView tv = new TextView(this);
    tv.setText("Hello World");
    FrameLayout customFrameLayout = (FrameLayout) findViewById(android.R.id.custom);
    customFrameLayout.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

以上我打电话addView(),其中最后一行抛出一个NullPointerException异常这让我觉得有一个与android.R.id.custom一个问题。

The last line of the above where I'm calling addView() throws a NullPointerException which makes me think there's a problem with android.R.id.custom.

所以现在的问题是,什么是错的上方,也有(通过使用Dialog类或延长它也许)创建一个自定义弹出一个更好的办法?

So the question is, what is wrong with the above and also is there a better way of creating a custom popup (perhaps by using the Dialog class or extending it)?

请注意:我只在这个例子作为测试使用TextView的,我想补充一些更复杂,我的实际弹出

NOTE: I'm only using TextView in this example as a test, I want to add something more complex for my actual popup.

推荐答案

一种选择是使用对话主题,以创建一个活动和风格是:

One option is to create an Activity and style it using the dialog theme:

<activity android:theme="@android:style/Theme.Dialog">

请参阅应用主题了解详情。

这篇关于自定义AlertDialog - 有问题的android.R.id.custom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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