简体   繁体   English

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

[英]Custom alert dialog with rounded corner and transparent background

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

Create your dialog like this像这样创建你的对话框

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                            context, R.style.CustomAlertDialog);
AlertDialog alertDialog = alertDialogBuilder.create();

In your styles.xml在你的styles.xml

<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowBackground">@drawable/popup_background</item>
</style>

popup_background.xml write whatever corner radius you want 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>

You can change corner radius.您可以更改拐角半径。 Good luck!祝你好运!

You can use the Material components for android library and the androidx.appcompat.app.AlertDialog .您可以使用android 库Material 组件androidx.appcompat.app.AlertDialog

Just use something like:只需使用类似的东西:

new MaterialAlertDialogBuilder(context)
            .setTitle("Dialog")
            .setMessage("Lorem ipsum dolor ....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null)
            .show();

Using a Material Components Theme you can customize the shape of your component with the shapeAppearanceOverlay attribute in your style.使用Material 组件主题,您可以使用样式中的shapeAppearanceOverlay属性自定义组件的形状

Something like:就像是:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
  </style>

Here you can define the rounded corners:您可以在此处定义圆角:

  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>

在此处输入图片说明

You Can create your customview by extending the alert dialog class.您可以通过扩展警报对话框类来创建自定义视图。

but i would recommend a PopupWindow or a subview that you show with animation when a specific action performed.但我会推荐一个PopupWindow或一个在执行特定操作时显示动画的子视图。

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

or you can make an activity with transparent background by adding this attribute to you Manifest.xml或者您可以通过将此属性添加到您的Manifest.xml来制作具有透明背景的活动

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

Try this,it worked for me like a charm试试这个,它对我很有用
I am working on sdkversion 28 with minimum sdk version 19我正在使用最低 sdk 版本 19 的 sdkversion 28

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

try it...尝试一下...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM