我试图在Android中生成一个自定义对话框。 我像这样创建我的Dialog:

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

除了对话框的标题,一切都很好。 即使我没有设置对话框的标题,对话框弹出窗口在对话框的位置有一个空白。

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

我尝试了一个AlertDialog,但似乎布局设置不正确:

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

如果我使用这段代码,我在最后一行得到一个空指针异常。对话框不是空,所以我试图检索的TextView不存在。 如果我取消注释我使用对话框构造函数的部分,一切正常,但对话框布局上面的标题。


当前回答

我不知道这个问题是否仍然存在,但在我的情况下,当我从Dialog切换到DialogFragment时,

requestWindowFeature(Window.FEATURE_NO_TITLE);

不是一个选择,但我可以用

setStyle(STYLE_NO_TITLE, 0);

结果是一样的。

其他回答

我想你现在可以用这个了:

AlertDialog dialog = new AlertDialog.Builder(this)
  .setView(view)
  .setTitle("")
  .create()

在代码中添加这一行

requestWindowFeature(Window.FEATURE_NO_TITLE);  

或者在XML中使用主题

android:theme="@android:style/Theme.NoTitleBar"

XML将是一个更好的实现,因为在代码版本中,创建标题栏,然后删除,这是一种资源浪费

好的,很好的尝试,但它不工作。我 得到: android.view.WindowManager BadTokenException美元: 无法添加窗口——令牌为空 如果我想,也不是为了申请 播放对话。

将警报对话框类型更改为系统对话框(例如,TYPE_SYSTEM_OVERLAY),看看这是否解决了您的问题

将整个对话框的“gravity”属性设置为“center”。然后,您将需要覆盖对话框中所有不希望居中的子组件的设置。

如果我们只是使用没有setTitle()的对话框,那么这将在删除标题的空间工作吗?

mUSSDDialog = new AlertDialog.Builder(context).setView(dialogView)
.setPositiveButton(R.string.send_button,DialogListener)
.setNegativeButton(R.string.cancel,DialogListener)
.setCancelable(false).create();

我找到了三种方法

1)使用requestWindowFeature

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE); 

2)使用style (style.xml)

<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
   <item name="android:windowNoTitle">true</item>
</style>

Dialog dialog = new Dialog(context, R.style.FullHeightDialog);

3)在AndroidManifest.xml中使用XML主题

 android:theme="@android:style/Theme.NoTitleBar"