我试图在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不存在。 如果我取消注释我使用对话框构造函数的部分,一切正常,但对话框布局上面的标题。


当前回答

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

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

其他回答

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

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

你可以不使用AlertDialog,通过定义从Dialog类扩展的新类来做到这一点:

public class myDialog extends Dialog {
    public myDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
}

您可以删除标题

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

其中dialog是我的对话框的名称。

我尝试requestWindowFeature(Window.FEATURE_NO_TITLE); 但不是为我工作,如果你和我一样,那就这样做:

向对话框传递主题可以为您删除标题栏。

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

将主题传递给你的对话框:

对话框对话框=新的对话框(这个,R.style.NoTitleDialog);

这很简单

下面是你可以用AlertBuilder做的事情来让标题消失:

TextView title = new TextView(this);
title.setVisibility(View.GONE);
builder.setCustomTitle(title);