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


当前回答

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

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

其他回答

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

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

在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 
    }
}

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

dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);


这个答案的以前版本,太复杂了:

您需要使用AlertDialog。在Android开发者的网站上有一个关于自定义对话框的很好的解释。

简而言之,您可以使用以下从官方网站复制的代码来完成此操作。这需要一个自定义布局文件,膨胀它,给它一些基本的文本和图标,然后创建它。然后使用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 = inflater.....如果是这样,那么你只需要改变一点点:而不是dialog.findView…使它为view.findView....一旦你完成了这些,记得使用dialog.show(),甚至builder.show(),而不用费力使用builder.create()。

在setcontentview之前使用以下代码

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

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

经过一系列的黑客攻击,我得到了这个:

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