Android 具有圆角和透明背景的自定义警报对话框
如何设计具有圆角和透明关闭按钮的自定义警报对话框?试试看Android 具有圆角和透明背景的自定义警报对话框,android,material-design,android-alertdialog,material-components,material-components-android,Android,Material Design,Android Alertdialog,Material Components,Material Components Android,如何设计具有圆角和透明关闭按钮的自定义警报对话框?试试看 final Dialog dialog = new Dialog(context); // Include dialog.xml file dialog.setContentView(R.layout.your_custom_layout); // Set dialog title //dialog.setTitle("Custom Dial
final Dialog dialog = new Dialog(context);
// Include dialog.xml file
dialog.setContentView(R.layout.your_custom_layout);
// Set 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 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 decline button is clicked, close the custom dialog
cancelnbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
可以通过扩展警报对话框类来创建customview 但我建议您使用弹出窗口或在执行特定操作时使用动画显示的子视图 或者,通过将此属性添加到Manifest.xml,您可以创建具有透明背景的活动
android:theme="@android:style/Theme.Translucent"
像这样创建对话框
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, R.style.CustomAlertDialog);
AlertDialog alertDialog = alertDialogBuilder.create();
在您的style.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>
您可以更改角半径。祝你好运 试试这个,它对我很有吸引力
我正在使用sdkversion 28和最小的sdk版本19 您可以使用和 只需使用以下内容:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
<!-- Alert Dialog -->
<style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
使用样式表,您可以使用样式中的shapeAppearanceOverlay
属性创建组件的样式
比如:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
<!-- Alert Dialog -->
<style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded
您可以在此处定义圆角:
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
圆的
8dp
我只需要圆角和透明的关闭按钮,而不是整个视图。提问需要显示最小值effort@Rasel你能帮我解决这个问题吗?首先谷歌一下……你可以找到。不要直接发布你的问题。可能重复的OP要求圆角和透明背景。你对角落有什么建议吗?我应该把这个代码放在哪里?行!我能找到的最好的选择。设置另一个drawable会修改对话框的大小,因此这不是一个选项如果我想使用渐变,我将如何实现它,先生?这个问题的答案应该可以帮助你,将弹出窗口背景改成渐变形状。这是一个很好的解决方案,但我面临的一个问题是,与原始警报对话框相比,对话框宽度发生了变化。您有任何相同的解决方案。使用警报对话框生成器,谢谢!