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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条