如何创建没有标题的对话框

如何创建没有标题的对话框

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

问题描述

限时删除!!

我试图在Android中生成一个自定义对话框。
我创建如下对话框:

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

除了对话框的标题之外,Everythings工作正常。
即使我没有设置对话框的标题,对话框弹出窗口在对话框的位置有空格。



有没有办法隐藏对话框的这部分?



我尝试使用AlertDialog,但似乎布局设置不正确:

  LayoutInflater inflater = 
(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看视图= 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);

如果我使用这个代码,我在最后一行得到一个空指针异常。对话框不为null,所以我尝试检索的TextView不存在。

如果我取消注释我使用Dialog构造函数的部分,一切都可以正常工作,但是在我的对话框布局之上。

解决方案

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



dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);






此答案的上一个版本,这是过于复杂的:



您需要使用 AlertDialog 。有关的Android开发人员网站有一个很好的解释。



在简短的总结中,您可以从官方网站下面的代码复制下面的代码。这需要一个自定义的layot文件,充气它,给它一些基本的文本和图标,然后创建它。你会用 alertDialog.show()显示它。

  AlertDialogBu​​ilder构建器
AlertDialog alertDialog;

上下文mContext = getApplicationContext();
LayoutInflater inflater =(LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
查看layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup)findViewById(R.id.layout_root));

TextView text =(TextView)layout.findViewById(R.id.text);
text.setText(你好,这是一个自定义对话框!);
ImageView image =(ImageView)layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

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

回应评论:



我假设具有id nr 的TextView在视图中正在膨胀, View view = inflater .... 。如果是这样,那么你需要改变一点:而不是 dialog.findView ... 使它 view.findView ... 。那么一旦你这样做,记得使用dialog.show(),甚至builder.show(),而不用去做builder.create()。


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?

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

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 version of this answer, which is overcomplicated:

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

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

In response to comment:

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:如何创建没有标题的对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:26