AlertDialog的自定义

来源:互联网 发布:舰队collection 知乎 编辑:程序博客网 时间:2024/04/30 12:46

众所周知,AlertDialog是android官方建议使用的Dialog.官方给了几个不同样式的AlertDialog,可有我们自由设置选择。但很多时候,在实际开发中,经常要设计到AlertDialog的自定义,不过,幸亏AlertDialog的自定义并不难。

1.AlertDialog.Builder builder=new AlerDialog.Builder();    //创建AlertDialog的构建器Builder.2.AlertDialog  dialog=builder.create();    //构建器builder通 过调用create()方法产生一个AlertDialog的实例dialog3.dialog.show();     //这时会弹出一个AlertDialog//接下来就是自定义AlertDialog//根据需要在XML文件中定义一个View//通过LayoutInflater.inflate(int ResLayoutId,ViewGroup parent)得到我们想要的View//在java文件中通过dialog.getWindow()获取显示出来的窗口//然后在该窗口setContentView()4.View view=LayoutInflater.from(this).inflate(R.layout.dialogView,null); //通常,我们获得view后还有通过findViewById(int id)找到其中各组件的引用,并设置相应的响应操作。5.dialog.getWindow().setContentView(view);  

当然,有另一种方法是直接在获取构建器Builder的实例builder后,通过builder.setView(View view)实现,但个人觉得这种方法并没有上面那种方法好,有时候还达不到理想的效果

                                             
0 0