【问题标题】:Android: How to create a Dialog without a title?Android:如何创建没有标题的对话框?
【发布时间】:2011-02-08 07:18:11
【问题描述】:

我正在尝试在 Android 中生成自定义对话框。 我像这样创建我的对话框:

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

【问题讨论】:

标签: android android-layout dialog


【解决方案1】:

FEATURE_NO_TITLE 在从头开始创建对话框时起作用,如下所示:

Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

但在创建 AlertDialog(或使用 Builder)时它不起作用,因为它已经禁用标题并在内部使用自定义标题。

我查看了 SDK 源代码,我认为它无法解决。因此,要删除顶部间距,唯一的解决方案是直接使用 Dialog 类从头开始创建自定义对话框 IMO。

另外,可以使用样式来做到这一点,例如在styles.xml 中:

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

然后:

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

【讨论】:

  • 我没有从头开始创建自定义对话框,而是按照 Oliverg 的建议创建了 styles.xml。然后,我将 android:theme="@style/FullHeightDialog" 添加到 Manifest 文件中的 ... 声明中。它刚刚奏效。谢谢..
  • @olivierg 但我想要一个带有全高对话框的按钮。解决办法是什么?
  • 注意行 requestWindowFeature 必须在 BEFORE setContentView 行。
  • 虽然这最好地回答了实际的 cmets,但在我看来,公认答案中的解决方案是最好的。我一开始就是这样做的,使用干净的Dialog,但创建AlertDialog 要容易得多。正如docs 中所述:The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses: &lt;AlertDialog and others described here&gt;AlertDialog 还以简单的方式处理生命周期内容和确定/取消。
【解决方案2】:

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

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


此答案的先前版本,过于复杂:

您需要使用AlertDialog。 Android 开发者网站上有一个关于custom dialogs 的很好的解释。

简而言之,您可以使用以下从官方网站复制的代码来执行此操作。这需要一个自定义的layot文件,对其进行膨胀,给它一些基本的文本和图标,然后创建它。然后你会用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();

回应评论:

我假设 ID 为 nr 的 TextView 位于您使用 View view = inflater.... 膨胀的视图中。如果是这样,那么您只需要更改一点:而不是 dialog.findView... 改为 view.findView...。然后,一旦你这样做了,记得使用 dialog.show(),甚至是 builder.show(),而不必费心去做 builder.create()。

【讨论】:

  • 我认为您可能误读了问题? Janusz 已经显示了自定义对话框,只需要删除标题的信息
  • 嗯,根据官方文档,“使用基本Dialog类制作的对话框必须有标题。如果你不调用setTitle(),那么用于标题的空间仍然是空的,但仍然可见。如果您根本不想要标题,那么您应该使用 AlertDialog 类创建自定义对话框。我没有亲自尝试过,但这表明即使使用自定义对话框布局或主题,也无法删除标题空间。
  • 第二个想法:我认为我们对“标题”的理解不同。我假设他说的是弹出窗口顶部的空间,而不是应用顶部的标题。
  • @SteveHaley,不可以使用以下行隐藏它dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  • 确保你做了 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);在你膨胀你的对话框视图之前。
【解决方案3】:

在你的代码中添加这一行

requestWindowFeature(Window.FEATURE_NO_TITLE);  

或者在 XML 中使用主题

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

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

好的尝试,但它不起作用。一世 得到: android.view.WindowManager$BadTokenException: 无法添加窗口 - 令牌 null 是 如果我愿意,不适合申请 显示对话框。

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

【讨论】:

  • 不要在 requestFeature() 之前调用 setContentView()。
【解决方案4】:

这样使用:

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

这将从对话框窗口中删除任何标题栏。

【讨论】:

  • 不要在 requestFeature() 之前调用 setContentView()。
  • 我以后怎么把它带回来?
【解决方案5】:

setcontentview之前使用下面的代码:-

    Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.custom_dialog);

注意:你必须有上面的代码,在相同的顺序和行。 requestWindowFeature 必须 setContentView 行之前。

【讨论】:

  • 在 Dialogfragment 中使用时,此解决方案对我来说效果更好,因为接受的答案会在对话框框架和内部内容视图之间产生微小的垂直间隙。
【解决方案6】:

你可以删除标题

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

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

【讨论】:

  • 不要在 requestFeature() 之前调用 setContentView()
【解决方案7】:

如果您在代码中使用requestWindowFeature(Window.FEATURE_NO_TITLE);,请确保它位于dialog.setContentView(); 之前,否则会导致应用程序崩溃。

【讨论】:

  • 相当怀疑之前尝试过,并且很惊讶它显然有效。因为在 android.developer.com 中,他们明确表示自定义对话框必须有标题。 :P
【解决方案8】:

我找到了三种方法来做到这一点>

1) 使用 requestWindowFeature

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

2) 使用样式(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"

【讨论】:

  • 方法一应该是 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
【解决方案9】:

在您的 Custom_Dialog.java 类中添加 requestWindowFeature(Window.FEATURE_NO_TITLE)

public class Custom_Dialog extends Dialog {

    protected Custom_Dialog(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
        requestWindowFeature(Window.FEATURE_NO_TITLE); //This line 
    }
}

【讨论】:

  • 这是唯一对我有用的东西......由于某种原因,所有其他建议都不起作用。我唯一推荐的是将构造函数公开,并提供其他只接受 Context 的 Dialog 构造函数
【解决方案10】:

olivierg's answer 为我工作,如果创建自定义 Dialog 类是您想要走的路线,这是最好的解决方案。但是,我无法使用 AlertDialog 类让我很困扰。我希望能够使用默认的系统 AlertDialog 样式。创建自定义对话框类不会有这种样式。

所以我找到了一个无需创建自定义类即可工作的解决方案(hack),您可以使用现有的构建器。

AlertDialog 在您的内容视图上方放置一个视图作为标题的占位符。如果找到视图并将高度设置为 0,空间就会消失。

到目前为止,我已经在 2.3 和 3.0 上对此进行了测试,它可能还不适用于每个版本。

这里有两个帮助方法:

/**
 * Show a Dialog with the extra title/top padding collapsed.
 * 
 * @param customView The custom view that you added to the dialog
 * @param dialog The dialog to display without top spacing
     * @param show Whether or not to call dialog.show() at the end.
 */
public static void showDialogWithNoTopSpace(final View customView, final Dialog dialog, boolean show) {
    // Now we setup a listener to detect as soon as the dialog has shown.
    customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // Check if your view has been laid out yet
            if (customView.getHeight() > 0) {
                // If it has been, we will search the view hierarchy for the view that is responsible for the extra space. 
                LinearLayout dialogLayout = findDialogLinearLayout(customView);
                if (dialogLayout == null) {
                    // Could find it. Unexpected.

                } else {
                    // Found it, now remove the height of the title area
                    View child = dialogLayout.getChildAt(0);
                    if (child != customView) {
                        // remove height
                        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
                        lp.height = 0;
                        child.setLayoutParams(lp);

                    } else {
                        // Could find it. Unexpected.
                    }
                }

                // Done with the listener
                customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
         }

    });

    // Show the dialog
    if (show)
             dialog.show();
}

/**
 * Searches parents for a LinearLayout
 * 
 * @param view to search the search from
 * @return the first parent view that is a LinearLayout or null if none was found
 */
public static LinearLayout findDialogLinearLayout(View view) {
    ViewParent parent = (ViewParent) view.getParent();
    if (parent != null) {
        if (parent instanceof LinearLayout) {
            // Found it
            return (LinearLayout) parent;

        } else if (parent instanceof View) {
            // Keep looking
            return findDialogLinearLayout((View) parent);

        }
    }

    // Couldn't find it
    return null;
}

这是一个如何使用它的示例:

    Dialog dialog = new AlertDialog.Builder(this)
        .setView(yourCustomView)
        .create();

    showDialogWithNoTopSpace(yourCustomView, dialog, true);

如果您将它与 DialogFragment 一起使用,请覆盖 DialogFragment 的 onCreateDialog 方法。然后像上面的第一个示例一样创建并返回您的对话框。唯一的变化是您应该将 false 作为第三个参数 (show) 传递,这样它就不会在对话框中调用 show()。 DialogFragment 稍后会处理。

例子:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new AlertDialog.Builder(getContext())
        .setView(yourCustomView)
        .create();

    showDialogWithNoTopSpace(yourCustomView, dialog, false);
    return dialog;
}

当我进一步测试时,我一定会更新所需的任何额外调整。

【讨论】:

  • 优雅的解决方案,+1。你知道如何在 DialogFragment 中使用它吗?
  • @Binoy 更新了 DialogFragments 的答案(这实际上是我个人使用它的方式)
【解决方案11】:

我不知道这个问题是否仍然存在,但就我而言,当我从 Dialog 切换到 DialogFragment 时,

requestWindowFeature(Window.FEATURE_NO_TITLE);

不是一个选项,但我可以使用

setStyle(STYLE_NO_TITLE, 0);

而是得到相同的结果。

【讨论】:

  • 澄清一下,这一行 (setStyle(STYLE_NO_TITLE, 0);) 将进入 DialogFragment 类的 onCreate 方法。
【解决方案12】:

使用 builder 将标题设置为空字符串。

    Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("");
...
    builder.show();

【讨论】:

    【解决方案13】:

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

    【讨论】:

      【解决方案14】:
      dialog=new Dialog(YourActivity.this, 1);  // to make dialog box full screen with out title.
      dialog.setContentView(layoutReference);
      dialog.setContentView(R.layout.layoutexample);
      

      【讨论】:

        【解决方案15】:

        在 XML 中使用主题

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

        【讨论】:

          【解决方案16】:

          如果我们只使用不带setTitle() 的对话框,那么这样可以删除标题的空格吗?

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

          【讨论】:

            【解决方案17】:

            认为您现在可以使用它:

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

            【讨论】:

              【解决方案18】:
              ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                                           "Loading. Please wait...", true);
              

              创建一个没有标题的对话框

              【讨论】:

                【解决方案19】:
                public static AlertDialog showAlertDialogWithoutTitle(Context context,String msg) 
                     {
                      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                      alertDialogBuilder.setMessage(msg).setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int id) {
                
                         }
                        });
                
                       return alertDialogBuilder.create(); 
                     }
                

                【讨论】:

                  【解决方案20】:

                  使用 AlertDialog 时,不使用 setTitle() 会使标题消失

                  【讨论】:

                    【解决方案21】:

                    经过一堆黑客攻击,我得到了这个工作:

                                Window window = dialog.getWindow();
                                View view = window.getDecorView();
                                final int topPanelId = getResources().getIdentifier( "topPanel", "id", "android" );
                                LinearLayout topPanel = (LinearLayout) view.findViewById(topPanelId);
                                topPanel.setVisibility(View.GONE);
                    

                    【讨论】:

                    • 这里的dialoggetResources() 是什么
                    【解决方案22】:

                    您可以在不使用 AlertDialog 的情况下执行此操作,方法是定义从 Dialog 类扩展的新类,如下所示:

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

                    【讨论】:

                      【解决方案23】:

                      您可以使用AlertBuilder 来使标题消失:

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

                      【讨论】:

                        【解决方案24】:

                        使用这个

                            Dialog dialog = new Dialog(getActivity());
                            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            dialog.setCancelable(true);
                            dialog.setContentView(R.layout.image_show_dialog_layout);
                        

                        【讨论】:

                          【解决方案25】:

                          dialog_custom .requestWindowFeature(Window.FEATURE_NO_TITLE);

                          这将从cutsom对话框中删除标题。

                          注意在添加内容之前添加这些行..例如

                               dialog_custom = Dialog(activity!!)
                              dialog_custom.requestWindowFeature(Window.FEATURE_NO_TITLE)
                              dialog_custom.setContentView(R.layout.select_vehicle_type)
                              dialog_custom.setCanceledOnTouchOutside(false)
                              dialog_custom.setCancelable(true)
                          

                          【讨论】:

                            【解决方案26】:

                            我尝试 requestWindowFeature(Window.FEATURE_NO_TITLE);
                            但对我不起作用,如果你和我一样那就这样做

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

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

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

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

                            就是这么简单

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2015-07-22
                              • 2022-01-20
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多