我试图在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时,不使用setTitle()会使标题消失

其他回答

使用这个

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

olivierg的答案对我来说是有效的,如果创建一个自定义Dialog类是你想要走的路线,这是最好的解决方案。但是,我不能使用AlertDialog类,这让我很困扰。我希望能够使用默认的系统AlertDialog样式。创建自定义对话框类不会有这种风格。

所以我找到了一个解决方案(黑客),将工作,而不必创建一个自定义类,你可以使用现有的构建器。

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

到目前为止,我已经在2.3和3.0上测试了这个功能,它可能还不能在每个版本上工作。

这里有两个helper方法:

/**
 * 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;
}

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

使用生成器将标题设置为空字符串。

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

像这样使用:

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

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

dialog=new Dialog(YourActivity.this, 1);  // to make dialog box full screen with out title.
dialog.setContentView(layoutReference);
dialog.setContentView(R.layout.layoutexample);