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 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形