yijidemomo 2012-10-17 02:23 采纳率: 100%
浏览 14382
已采纳

Android:怎么创建一个没有title的dialog?

我试图在android生成一个自定义的对话框。我像下边这样创建我的对话框:

dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);

除了对话框的title,其他的都很好。就算我不设置对话框的title,当对话框弹出的时候仍然有一个空白的地方。
有什么方法可以隐藏掉这个空白的地方么?
我用 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不存在。
如果在我使用对话框的构造函数部分取消注释的部分,一切都很好,除了我的对话框布局上的title部分。

  • 写回答

3条回答 默认 最新

  • Sueyexin 2012-10-17 02:56
    关注

    你需要使用到 AlertDialog。

    在这么短的总结,你的代码就像是从官网中复制的一样。那需要一个自定义布局文件,给它一些基本的文本和图标,然后创建它。然后显示它,再用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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?