Bel*_*bub 14

像这样创建对话框

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                            context, R.style.CustomAlertDialog);
AlertDialog alertDialog = alertDialogBuilder.create();
Run Code Online (Sandbox Code Playgroud)

styles.xml中

    <style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowBackground">@drawable/popup_background</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这对角球半径好运


Gab*_*tti 14

您可以将Material 组件用于 android 库androidx.appcompat.app.AlertDialog.

只需使用类似的东西:

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

使用Material Components 主题,您可以使用样式中的属性自定义组件的形状shapeAppearanceOverlay

就像是:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

您可以在此处定义圆角:

  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 作品!我能找到的最好的选择。设置另一个可绘制对象会修改对话框的大小,因此它不是一个选项 (2认同)

小智 -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();
     }
 });
Run Code Online (Sandbox Code Playgroud)