时间:2023-01-25 10:07:17 | 栏目:Android代码 | 点击:次
本文实例为大家分享了Android自定义弹框Dialog效果的具体代码,供大家参考,具体内容如下
1.dialog_delete.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="236dp" android:layout_height="184dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/dialog_white_back"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除设备" android:textColor="#333333" android:textSize="15sp" android:layout_centerHorizontal="true" android:layout_marginTop="13dp"></TextView> <ImageView android:id="@+id/delete_close_id" android:layout_width="10dp" android:layout_height="10dp" android:background="@mipmap/login_close_back" android:layout_alignParentRight="true" android:layout_marginTop="16dp" android:layout_marginRight="13dp"></ImageView> <TextView android:id="@+id/delete_device_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设备 ID:123456789" android:textColor="#333333" android:textSize="16sp" android:layout_marginTop="75dp" android:layout_centerHorizontal="true"></TextView> <TextView android:id="@+id/delete_cancle_id" android:layout_width="77dp" android:layout_height="26dp" android:background="@drawable/round_gray" android:layout_alignParentBottom="true" android:layout_marginBottom="18dp" android:layout_marginLeft="31dp" android:text="取消" android:textSize="11sp" android:textColor="#333333" android:gravity="center"></TextView> <TextView android:id="@+id/delete_sure_id" android:layout_width="77dp" android:layout_height="26dp" android:background="@drawable/round_blue" android:layout_alignParentBottom="true" android:layout_marginBottom="18dp" android:layout_alignParentRight="true" android:layout_marginRight="32dp" android:text="确定" android:textSize="11sp" android:textColor="#FEFDFD" android:gravity="center"></TextView> </RelativeLayout> </RelativeLayout>
2.设置背景边框,在drawable创建dialog_white_back.xml
颜色以及圆角 根据自己需求修改
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color ="#ffffff"/> <corners android:radius="8dp"/> </shape>
3.按钮的背景边框,在drawable创建round_gray.xml
颜色以及圆角 根据自己需求修改
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color = "#DCDCDC" /> <corners android:radius="360dp"/> </shape>
4.在Style文件中添加
<!--Dialog 样式 --> <style name="BottomDialog" parent="AlertDialog.AppCompat"> <item name="android:windowIsFloating">true</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:fullBright">@color/clear</item> <item name="android:fullDark">@color/clear</item> <item name="android:topBright">@color/clear</item> <item name="android:topDark">@color/clear</item> <item name="android:borderlessButtonStyle">@color/clear</item> </style>
5.Java代码部分
/** * 删除AlertDialog */ public void DeleteDialog() { //布局、id View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_delete, null); ImageView delete_close_id = view.findViewById(R.id.delete_close_id); TextView delete_device_id = view.findViewById(R.id.delete_device_id); TextView delete_cancle_id = view.findViewById(R.id.delete_cancle_id); TextView delete_sure_id = view.findViewById(R.id.delete_sure_id); //显示样式 final Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog); dialog.setContentView(view); dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); DisplayMetrics dm = getResources().getDisplayMetrics(); int displayWidth = dm.widthPixels; int displayHeight = dm.heightPixels; android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); //获取对话框当前的参数值 p.width = (int) (displayWidth * 0.8); //宽度设置为屏幕的0.5 //dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失 dialog.getWindow().setAttributes(p); //设置生效 dialog.show(); //点击关闭 delete_close_id.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); //点击确定删除 delete_sure_id.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); //点击取消删除 delete_cancle_id.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); }