如何删除警报对话框标题栏

How to remove Alert dialog Title bar

我正在按照 this 代码创建自定义对话框,但我不知道如何删除对话框标题栏?

  AlertDialog alertDialog;

   @Override
   protected Dialog onCreateDialog(int id) {

      AlertDialog dialogDetails = null;

      switch (id) {
      case DIALOG_LOGIN:

       LayoutInflater inflater = LayoutInflater.from(this);
       View dialogview = inflater.inflate(R.layout.dialog_layout, null);

           AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
           dialogbuilder.setTitle("Login");
           dialogbuilder.setView(dialogview);
           dialogDetails = dialogbuilder.create();

           break;
          }

      return dialogDetails;
     }

     @Override
     protected void onPrepareDialog(int id, Dialog dialog) {

      switch (id) {
      case DIALOG_LOGIN:
      alertDialog = (AlertDialog) dialog;

      .......

}

我知道要删除警报对话框的标题区域,我们必须使用 requestWindowFeature(Window.FEATURE_NO_TITLE);

但不知道我必须把线放在什么地方?

如果您不想在警告对话框中显示标题栏,则只需从代码中删除以下行。

dialogbuilder.setTitle("Login");

如果仍然无法正常工作,请添加以下行。

dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.setContentView(R.layout.logindialog); 之前使用 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 这样您就可以隐藏 Dialog 的标题。

只需删除

dialogbuilder.setTitle("Login");

使用这个

AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
           dialogbuilder .requestWindowFeature(Window.FEATURE_NO_TITLE);
           dialogbuilder.setView(dialogview);
           dialogDetails = dialogbuilder.create();

试试这个::

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

Refer here

使用dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

首先删除这一行:

dialogbuilder.setTitle("Login");

然后添加这个:

dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);

我无法通过 AlertDialog Builder 找到 .requestWindowFeature

如果在使用生成器构建警报对话框时不想有标题,请使用 new AlertDialog.Builder(getContext(), android.R.style.Theme_Material_Light_Dialog_NoActionBar_MinWidth);

最终在摩托罗拉 Droid Razr M (AOS 4.4) 智能手机上,这些方法不起作用,我相信市场上还有其他类似的智能手机。唯一获得的效果来自:

        setTitle(null);
        setCustomTitle(null);

标题没有文本,但它的视图仍然存在于对话框视图中(看起来像顶部的空视图)。所以我找到的唯一方法是基于 this answer:

        int titleId = getContext().getResources().getIdentifier( "alertTitle", "id", "android" );
        if (titleId > 0) {
            View dialogTitle = findViewById(titleId);
            if (dialogTitle != null) {
                ((View)dialogTitle.getParent().getParent()).setVisibility(View.GONE);
            }
        }

使用带有自定义布局的 Dialog 肯定会有效

final Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_layout);
            Button btOk = dialog.findViewById(R.id.btOk);
            Button btCancel = dialog.findViewById(R.id.btCancel);
            btOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //To Do
                    dialog.dismiss();
                }
            });
            btCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.setCancelable(false);
            dialog.show();

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Ok" />
    <Button
        android:id="@+id/btCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />
</LinearLayout>

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

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

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

Dialog dialog = new Dialog(this, R.style.NoTitleDialog);