我该如何设计一个带圆角和透明关闭按钮的自定义警告对话框?
我该如何设计一个带圆角和透明关闭按钮的自定义警告对话框?
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, R.style.CustomAlertDialog);
AlertDialog alertDialog = alertDialogBuilder.create();
在你的styles.xml文件中:
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/popup_background</item>
</style>
在文件popup_background.xml中,写入您想要的任何圆角半径:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="6dp" />
</shape>
你可以更改角落半径。
androidx.appcompat.app.AlertDialog
。只需像这样使用:new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
使用Material Components主题,您可以通过样式中的shapeAppearanceOverlay
属性自定义组件形状。
例如:
<!-- Alert Dialog -->
<style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
在这里,您可以定义圆角:
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
你可以通过扩展警报对话框类来创建自定义视图。
但我建议使用PopupWindow或子视图,在执行特定操作时以动画方式显示。
或者,您可以通过将此属性添加到您的Manifest.xml文件中,使活动具有透明背景:
android:theme="@android:style/Theme.Translucent"
试试这个。对我来说非常有效。
我正在使用sdk版本28,最低SDK版本为19。
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
试一下...
final Dialog dialog = new Dialog(context);
// Include the dialog.xml file
dialog.setContentView(R.layout.your_custom_layout);
// Set the dialog title
//dialog.setTitle("Custom Dialog");
// Set values for custom dialog components -
// text, image and button
final EditText name = (EditText) dialog.findViewById(R.id.name_edit);
dialog.show();
/
Button editButton = (Button) dialog.findViewById(R.id.editbtn);
// If the decline button is clicked, close the custom dialog
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
final Button cancenbtn = (Button) dialog.findViewById(R.id.cancelbtn);
// If the decline button is clicked, close the custom dialog
cancelnbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});