Android的:如何创建一个对话框没有标题? [英] Android: How to create a Dialog without a title?

查看:103
本文介绍了Android的:如何创建一个对话框没有标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成的Andr​​oid定制对话框。 创建我的对话是这样的:

I'm trying to generate a custom dialog in Android. I create my Dialog like this:

dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);

万物工作正常,除了对话框的标题。 即使我不设置对话框的标题对话框弹出具有在对话框的位置的空格。

Everythings works fine except for the title of the Dialog. Even if I don't set the title of the dialog the dialog popup has a blank space at the position of the dialog.

有什么办法来隐藏这部分的对话吗?

Is there any way to hide this part of the Dialog?

我试过跟一个AlertDialog但似乎布局设置不正确:

I tried it with an AlertDialog but it seems the layout is not set properly:

LayoutInflater inflater = 
    (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);

// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);

dialog = builder.create();

((TextView) dialog.findViewById(R.id.nr)).setText(number);

如果我用这个code,我得到的最后一行空指针异常。该对话框不为空所以TextView的我尝试检索不存在。
如果我取消部分地方我用的是对话构造一切工作正常,但上面我的对话框布局的称号。

If I use this code I get a null Pointer Exception in the last line. The dialog is not null so the TextView I try to retrieve does not exist.
If I uncomment the part where I use the Dialog Constructor everything works fine but for the title above my dialog layout.

推荐答案

您可以使用隐藏对话框的标题:

You can hide the title of a dialog using:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

previous版本的答案,这是过于复杂的:

Previous version of this answer, which is overcomplicated:

您需要使用 AlertDialog 。有对Android开发者网站了解自定义对话框一个很好的解释。

You need to use an AlertDialog. There's a good explanation on the Android Developer's site about custom dialogs.

在很短的总结,你这样做与code像从官方网站下面的复制。这需要一个定制layot​​文件,膨胀它,给它一些基本的文本和图标,然后创建它。你会表现出来,然后用 alertDialog.show()

In very short summary, you do this with code like copied below from the official website. That takes a custom layot file, inflates it, gives it some basic text and icon, then creates it. You'd show it then with alertDialog.show().

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
        mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
        (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

在回应评论:

我认为TextView的id为 NR 是你用充气视图中查看=吹气...... 。如果是这样,那么你需要改变只是一个位:代替 dialog.findView ... view.findView ... 。然后,一旦你做到了这一点,记得使用dialog.show(),甚至builder.show()而没有做builder.create()。

I assume that TextView with the id nr is in the View you are inflating with View view = inflater..... If so, then you need to change just one bit: instead of dialog.findView... make it view.findView.... Then once you've done that, remember to use dialog.show(), or even builder.show() without bothering to do builder.create().

这篇关于Android的:如何创建一个对话框没有标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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