【问题标题】:How to remove Alert dialog Title bar如何删除警报对话框标题栏
【发布时间】:2015-04-24 10:18:03
【问题描述】:

我正在关注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);

但不知道我必须在哪里放置在线上方?

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

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

dialogbuilder.setTitle("Login");

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

dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);

【讨论】:

  • 如果我希望我的文字在(手机和平板电脑)中看起来都一样?
  • 您也可以创建自定义对话框。
  • 嘿兄弟这次我不是在谈论自定义警报对话框,我关心的是如何在手机和平​​板电脑中始终显示相同大小的文本
  • 我们无法自定义默认警报对话框。所以你必须为此创建自定义对话框。
  • 兄弟请尝试理解我的观点,我不是在这里谈论对话,我的问题就像在我的活动中我有文本视图,文本为“Hello World”,我希望它看起来应该大小相同在所有设备(手机/标签)上
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    删除

    dialogbuilder.setTitle("Login");
    

    【讨论】:

      【解决方案4】:

      使用这个

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

      【讨论】:

      • "dialogbuilder.requestWindowFeature" 不受支持。
      • 对话框对话框=新对话框(上下文); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      • @AshishJohn:在评论任何垃圾之前阅读完整的问题和标题! Alert Dialogue 和 Dialogue 是两个不同的类
      • @BhavikMehta 给定的问题是“....但我没有得到如何删除对话框标题栏”; &我提出了删除对话框标题栏的解决方案...我认为您只阅读了编写“警报对话”的代码而不是问题
      【解决方案5】:

      试试这个::

      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

      【讨论】:

        【解决方案6】:

        使用dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        【讨论】:

          【解决方案7】:

          首先删除这一行:

          dialogbuilder.setTitle("Login");
          

          然后添加这个:

          dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);
          

          【讨论】:

            【解决方案8】:

            我无法使用 AlertDialog Builder 找到 .requestWindowFeature

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

            【讨论】:

              【解决方案9】:

              最终在摩托罗拉 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);
                          }
                      }
              

              【讨论】:

                【解决方案10】:

                将 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>
                

                【讨论】:

                  猜你喜欢
                  • 2011-09-09
                  • 2012-10-28
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-11-17
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多