Dialog提示框演示
Dialog样式(去除自定义dialog四角默认色)
样式一
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- dialog样式 --> <style name="DialogTheme" parent="@android:style/Theme.Dialog"> <!-- 边框 --> <item name="android:windowFrame">@null</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 半透明 --> <item name="android:windowIsTranslucent">true</item> <!-- 无标题 --> <item name="android:windowNoTitle">true</item> <item name="android:background">@android:color/transparent</item> <!-- 背景透明 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 模糊 --> <item name="android:backgroundDimEnabled">true</item> <!-- 遮罩层 --> <item name="android:backgroundDimAmount">0.5</item> </style> </resources>
样式二
<resources xmlns:tools="http://schemas.android.com/tools"> <style name="DialogTheme" parent="@android:style/Theme.DeviceDefault.Light.Dialog"> <item name="android:windowFrame">@null</item><!-- 边框 --> <item name="android:windowIsFloating">true</item><!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item><!-- 半透明 --> <item name="android:windowNoTitle">true</item><!-- 无标题 --> <item name="android:backgroundDimEnabled">false</item><!-- 背景模糊 --> <item name="android:windowBackground">@android:color/transparent</item><!-- 背景色 --> </style> </resources>
引用方法
// 样式一 Dialog dialog = new Dialog(context, R.style.DialogTheme); // 样式二 AlertDialog dialog = new AlertDialog.Builder(activity,R.style.DialogTheme).setView(view).show();
ialog方法
设置不可点击dialog以外的区域
// 设置不可点击dialog以外的取余 dialog.setCanceledOnTouchOutside(false);
dialog去掉头部标题栏
// dialog去掉头部标题栏(去掉标题栏这句一定要写在setContentView()方法的前面,不然会报错) dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog设置显示位置
// dialog设置居中显示 dialog.getWindow().setGravity(Gravity.CENTER);
dialog解决不能居中
// 原因是dialog里面也是有头部标题栏存在的,去掉头部标题栏,再设置居中显示即可 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); Window mWindow = dialog.getWindow(); mWindow.setGravity(Gravity.CENTER);
dialog设置偏移量
dialog.getWindow().getAttributes().x = 25;
动态设置dialog宽高
if (myDataList.size() == 1) { dialog.getWindow().setLayout(600, 600); } else { dialog.getWindow().setLayout(600, 900); }
监测dialog是否dismiss(可用于监听返回键)
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { imageView.setVisibility(View.VISIBLE); textView.setVisibility(View.VISIBLE); } });
简单的取消重试Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(myContext); builder.setMessage("初始化失败,请重试"); builder.setPositiveButton("重试", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); init(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }); builder.show();
自定义Dialog
public static void showDialog(Activity activity) { if (myDataList == null) { return; } View view = activity.getLayoutInflater().inflate(R.layout.my_dialog, null); AlertDialog dialog = new AlertDialog.Builder(activity, R.style.DialogTheme).setView(view).show(); dialog.setCanceledOnTouchOutside(false); if (dialog.getWindow() == null) { return; } dialog.getWindow().setGravity(Gravity.CENTER); if (myDataList.size() == 1) { dialog.getWindow().setLayout(600, 600); } else { dialog.getWindow().setLayout(600, 900); } ImageView close = view.findViewById(R.id.iv_close); TextView name = view.findViewById(R.id.tv_name); ListView lvData = view.findViewById(R.id.lv_data); close.setOnClickListener(v -> { dialog.dismiss(); }); // 按照序号排序 myDataList.sort(Comparator.comparing(MyData::getNum)); name.setText(myDataList.get(0).getName()); UserInfoAdapter userInfoAdapter = new UserInfoAdapter(activity, myDataList); lvData.setAdapter(userInfoAdapter); }