带有圆角和透明背景的自定义警告对话框

5

我该如何设计一个带圆角和透明关闭按钮的自定义警告对话框?


我只需要圆角和透明的取消按钮,而不是整个视图。 - Braj Bhushan Singh
提问需要展示最小的努力。 - Rasel
@Rasel,你能帮我解决这个问题吗? - Braj Bhushan Singh
先谷歌一下..你可以找到答案。不要直接发布你的问题。https://dev59.com/zF4b5IYBdhLWcg3wlihP - Vishal Jadav
可能是如何实现自定义AlertDialog视图的重复问题。 - Braj Bhushan Singh
5个回答

34
创建对话框的方式如下:
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>

你可以更改角落半径。


如果我想使用梯度,我该如何实现呢? - Lidor Eliyahu Shelef
1
这个问题的答案应该可以帮助你把弹出框背景改为渐变形状。 - Belzebub
这是一个相当不错的解决方案,但我遇到了一个问题,就是对话框的宽度与原始警告对话框相比发生了变化。你有什么解决方案吗? - Kishan Donga

16
您可以使用 Material components for Android libraryandroidx.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>

在此输入图片描述


2
可以用!我能找到的最佳替代品。设置另一个可绘制对象会修改对话框框的大小,因此这不是一个选项。 - Nicolás Vera
1
多好的答案,这应该是正确的答案,我同意@NicolásVera - MusabAlothman

0

你可以通过扩展警报对话框类来创建自定义视图。

但我建议使用PopupWindow或子视图,在执行特定操作时以动画方式显示。

或者,您可以通过将此属性添加到您的Manifest.xml文件中,使活动具有透明背景:

  android:theme="@android:style/Theme.Translucent"

0

试试这个。对我来说非常有效。

我正在使用sdk版本28,最低SDK版本为19。

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

OP要求圆角和透明背景。你有什么关于圆角的建议吗? - Bö macht Blau

-2

试一下...

 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();
     }
 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接