【问题标题】:Custom alert dialog with rounded corner and transparent background具有圆角和透明背景的自定义警报对话框
【发布时间】:2017-06-02 20:32:06
【问题描述】:

如何设计带有圆角和透明关闭按钮的自定义警报对话框?

【问题讨论】:

标签: android material-design android-alertdialog material-components material-components-android


【解决方案1】:

像这样创建你的对话框

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>

您可以更改拐角半径。祝你好运!

【讨论】:

  • 如果我想使用渐变,我将如何完成?
  • stackoverflow.com/questions/13929877/…这个问题的答案应该对你有帮助,把popup_background改成渐变形状。
  • 这是一个非常好的解决方案,但与原始警报对话框相比,我面临的一个问题是对话框宽度发生了变化。你有任何相同的解决方案。
【解决方案2】:

您可以使用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 Theme,您可以在您的样式中使用customize the shape 组件的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>

【讨论】:

  • 有效!我能找到的最佳选择。设置另一个drawable会修改对话框的大小,所以它不是一个选项
【解决方案3】:

试试这个,它对我很有用
我正在使用 sdkversion 28,最低 sdk 版本为 19

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

【讨论】:

  • OP 要求圆角 透明背景。你对角落有什么建议吗?
【解决方案4】:

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

但我会推荐一个 PopupWindow 或子视图,您可以在执行特定操作时显示动画。

https://developer.android.com/reference/android/widget/PopupWindow.html

或者您可以通过向您添加此属性来制作具有透明背景的活动 Manifest.xml

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

【讨论】:

    【解决方案5】:

    试试看……

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

    【讨论】:

      猜你喜欢
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多