<强>前言:强>
项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框。
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " android:取向=按怪薄盿ndroid: layout_width=癿atch_parent” xmlns:最适合的=" http://schemas.android.com/apk/res-auto " android: layout_height=" wrap_content " android:背景=" @drawable/dialog_corner_bg” android: paddingBottom=" @dimen/dp_16” 比; & lt; ImageView android: id=癅 + id/dialog_img” android: layout_width=" 30 dp " android: layout_height=" 30 dp " android: src=" https://www.yisu.com/zixun/@mipmap icon1 " android: layout_marginTop=" @dimen/dp_12” android: layout_centerHorizontal=" true "/比; & lt; me.grantland.widget.AutofitTextView android: id=癅 + id/dialog_txt_content” android: layout_width=" match_parent " android: layout_height=" wrap_content " android:单行模式=" true " android: maxLines=" 1 " android: textSize=" 14 sp " 最适合的:minTextSize=" 10 sp” android:文本="下载失败,请重试” android:重力="中心" android: layout_margin=" @dimen/dp_6” android: layout_centerInParent=" true "/比; & lt; TextView android: id=癅 + id/dialog_btn_comfirm” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本="确定” android:重力="中心" android:背景=" @drawable/bg_btn_blue_big” android:输入textColor=" @color/白” android: paddingTop=" @dimen/dp_6” android: paddingBottom=" @dimen/dp_6” android: paddingLeft=" @dimen/dp_30” android: paddingRight=" @dimen/dp_30” android: layout_centerHorizontal=" true " android: layout_alignParentBottom=" true "/比; & lt;/RelativeLayout> >之前
其中根容器用到,
android:背景=" @drawable/dialog_corner_bg "
这是形状来设置边缘圆角
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; !——用于设置信息对话框的圆角——比; & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android "比; & lt;角落android:半径=" @dimen/dp_12”祝辞& lt;/corners> & lt;固体android:颜色=" @color/白”祝辞& lt;/solid> & lt;/shape>
2,从上面可以看到设置对话框的圆角只需要一个可拉的文件,形状设置角落属性即可。
也许大家从别的文章发现,有的人用圆角背景图片来实现对话框圆角,有的人用样式来实现对话框圆角。
经过我1个多小时的折腾,发现这些方法都不可靠,其实很简单,以上方法用的是AlertDialog,但是我们这里用的对话框类,一个形状足矣。
因为项目中必定多出用到对话框,所以我写一个静态方法,传上下文参数和提示文本的内容即可:
公共静态孔隙showEditDialog(上下文语境,字符串消息){}
1,初始化对话框相关操作:
视图视图=LayoutInflater.from(上下文).inflate (R.layout.dialog_message, null); TextView确认;//确定按钮 最后一个TextView内容;//内容 确认=(TextView) view.findViewById (R.id.dialog_btn_comfirm); 内容=(TextView) view.findViewById (R.id.dialog_txt_content); content.setText(消息); 最后一个对话框对话框=new对话框(上下文); dialog.setContentView(查看); dialog.getWindow () .setBackgroundDrawableResource (android.R.color.transparent);//设置对话框背景透明,对于AlertDialog就不管用了Android实现自定义圆角对话框对话框的示例代码