【问题标题】:How to make custom dialog with rounded corners in android如何在android中制作带有圆角的自定义对话框
【发布时间】:2015-05-10 07:40:48
【问题描述】:

我正在尝试做的事情:我正在尝试在 android 中制作一个带有圆角的自定义对话框。

发生了什么:我可以制作自定义对话框,但它没有圆角。我尝试添加一个选择器,但仍然无法实现圆角。

下面是我的代码:


Java 代码:

private void launchDismissDlg() {

        dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dlg_dismiss);
        dialog.setCanceledOnTouchOutside(true);

        Button btnReopenId = (Button) dialog.findViewById(R.id.btnReopenId);
        Button btnCancelId = (Button) dialog.findViewById(R.id.btnCancelId);

        btnReopenId.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });


        btnCancelId.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="&quot;I WOULD LIKE TO DISMISS THE VENDOR&quot;"
                android:textColor="@color/col_dlg_blue_light"
                android:textSize="14sp"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="center" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="BECAUSE"
                android:textColor="@android:color/black"
                android:textStyle="bold" />
        </TableRow>



        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btnReopenId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/col_dlg_green_light"
                android:text="REOPEN"
                android:padding="5dp"
                android:textSize="14sp"
                android:textColor="@android:color/white"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnCancelId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/col_dlg_pink_light"
                android:text="CANCEL"
                android:padding="5dp"
                android:textSize="14sp"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
        </TableRow>
    </TableLayout>

</LinearLayout>

【问题讨论】:

标签: android android-custom-view android-dialog


【解决方案1】:

在drawable中创建一个XML文件,比如dialog_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/white"/>
    <corners
        android:radius="30dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

将其设置为布局 XML 中的背景:

android:background="@drawable/dialog_bg"

将对话框的根视图的背景设置为透明,因为 Android 会将对话框布局放在根视图中,该根视图会隐藏自定义布局中的角。

Java:

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

科特林:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

【讨论】:

  • 这种方法的问题是按钮的背景也会是透明的。
  • dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 是大多数其他 SO 线程所缺少的! +1
  • setBackgroundDrawable 似乎已被弃用......还有其他选择吗?
  • setContentView之前添加dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  • 除了显示对话框按钮的部分是透明的以外,此方法有效:/
【解决方案2】:

您需要执行以下操作:

  • 为对话框的背景创建一个圆角背景:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
        <solid android:color="#fff" />
    
        <corners
            android:bottomLeftRadius="8dp"
            android:bottomRightRadius="8dp"
            android:topLeftRadius="8dp"
            android:topRightRadius="8dp" />
    
    </shape>
    
  • 现在在根布局中的对话框的 XML 文件中,使用具有所需边距的背景:

    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:background="@drawable/dialog_background"
    
  • 最后在java部分你需要这样做:

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(layoutResId);
    View v = getWindow().getDecorView();
    v.setBackgroundResource(android.R.color.transparent);
    

这对我来说非常适合。

【讨论】:

  • 如果你想对所有角落使用相同的值,那么你应该使用 &lt;corners android:radius="8dp"/&gt; ,否则你会遇到渲染问题
  • @Mehran 好点。感谢您指出了这一点。你也可以分享这个信息的来源吗?
  • @SMR Path.isConvex is not supported 如果您对所有角使用具有相同值的形状,则通常会在布局中收到警告。 stackoverflow.com/q/34736981/6605907
【解决方案3】:

使用 Androidx 库和 Material Components Theme,您可以覆盖 getTheme() 方法:

import androidx.fragment.app.DialogFragment

class RoundedDialog: DialogFragment() {

    override fun getTheme() = R.style.RoundedCornersDialog

    //....

}

与:

<style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
    <item name="dialogCornerRadius">16dp</item>
</style>

或者您可以使用材料组件库中包含的MaterialAlertDialogBuilder

import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class RoundedAlertDialog : DialogFragment() {

    //...

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return MaterialAlertDialogBuilder(requireActivity(), R.style.MaterialAlertDialog_rounded)
                .setTitle("Test")
                .setMessage("Message")
                .setPositiveButton("OK", null)
                .create()
    }

}

与:

<style name="MaterialAlertDialog_rounded" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/DialogCorners</item>
</style>

<style name="DialogCorners">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
</style>

如果您不需要DialogFragment,只需使用MaterialAlertDialogBuilder

【讨论】:

  • 这个对我有用。你为我节省了很多时间。谢谢:)
  • 很棒的答案,完美!
【解决方案4】:

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

这对我有用

【讨论】:

    【解决方案5】:

    如果您使用材料组件

    CustomDialog.kt

    class CustomDialog: DialogFragment() {
    
        override fun getTheme() = R.style.RoundedCornersDialog
    
    }
    

    styles.xml

    <style name="RoundedCornersDialog" parent="Theme.MaterialComponents.Dialog">
        <item name="dialogCornerRadius">dimen</item>
    </style>
    

    【讨论】:

    • 这是我的解决方案,谢谢!就我而言,我添加了文本和背景颜色:
    【解决方案6】:

    dimen.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <integer name="weight">1</integer>
    
        <dimen name="dialog_top_radius">21dp</dimen>
    
        <dimen name="textview_dialog_head_min_height">50dp</dimen>
        <dimen name="textview_dialog_drawable_padding">5dp</dimen>
    
        <dimen name="button_dialog_layout_margin">3dp</dimen>
    
    
    </resources>
    

    styles.xml

    <style name="TextView.Dialog">
            <item name="android:paddingLeft">@dimen/dimen_size</item>
            <item name="android:paddingRight">@dimen/dimen_size</item>
            <item name="android:gravity">center_vertical</item>
            <item name="android:textColor">@color/black</item>
        </style>
    
        <style name="TextView.Dialog.Head">
            <item name="android:minHeight">@dimen/textview_dialog_head_min_height</item>
            <item name="android:textColor">@color/white</item>
            <item name="android:background">@drawable/dialog_title_style</item>
            <item name="android:drawablePadding">@dimen/textview_dialog_drawable_padding</item>
        </style>
    
        <style name="TextView.Dialog.Text">
            <item name="android:textAppearance">@style/Font.Medium.16</item>
        </style>
    
        <style name="Button" parent="Base.Widget.AppCompat.Button">
            <item name="android:layout_height">@dimen/button_min_height</item>
            <item name="android:layout_width">match_parent</item>
            <item name="android:textColor">@color/white</item>
            <item name="android:gravity">center</item>
            <item name="android:textAppearance">@style/Font.Medium.20</item>
        </style>
    
     <style name="Button.Dialog">
            <item name="android:layout_weight">@integer/weight</item>
            <item name="android:layout_margin">@dimen/button_dialog_layout_margin</item>
        </style>
    
        <style name="Button.Dialog.Middle">
            <item name="android:background">@drawable/button_primary_selector</item>
        </style>
    

    dialog_title_style.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <gradient
            android:angle="270"
            android:endColor="@color/primaryDark"
            android:startColor="@color/primaryDark" />
    
        <corners
            android:topLeftRadius="@dimen/dialog_top_radius"
            android:topRightRadius="@dimen/dialog_top_radius" />
    
    </shape>
    

    dialog_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/backgroundDialog" />
        <corners
            android:topLeftRadius="@dimen/dialog_top_radius"
            android:topRightRadius="@dimen/dialog_top_radius" />
        <padding />
    </shape>
    

    dialog_one_button.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dailog_background"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/dialogOneButtonTitle"
            style="@style/TextView.Dialog.Head"
            android:text="Process Completed" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/dialogOneButtonText"
                style="@style/TextView.Dialog.Text"
                android:text="Return the main menu" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <Button
                    android:id="@+id/dialogOneButtonOkButton"
                    style="@style/Button.Dialog.Middle"
                    android:text="Ok" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </LinearLayout>
    

    OneButtonDialog.java

    package com.example.sametoztoprak.concept.dialogs;
    
    import android.app.Dialog;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.example.sametoztoprak.concept.R;
    import com.example.sametoztoprak.concept.models.DialogFields;
    
    /**
     * Created by sametoztoprak on 26/09/2017.
     */
    
    public class OneButtonDialog extends Dialog implements View.OnClickListener {
    
        private static OneButtonDialog oneButtonDialog;
        private static DialogFields dialogFields;
    
        private Button dialogOneButtonOkButton;
        private TextView dialogOneButtonText;
        private TextView dialogOneButtonTitle;
    
        public OneButtonDialog(AppCompatActivity activity) {
            super(activity);
        }
    
        public static OneButtonDialog getInstance(AppCompatActivity activity, DialogFields dialogFields) {
            OneButtonDialog.dialogFields = dialogFields;
            return oneButtonDialog = (oneButtonDialog == null) ? new OneButtonDialog(activity) : oneButtonDialog;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.dialog_one_button);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    
            dialogOneButtonTitle = (TextView) findViewById(R.id.dialogOneButtonTitle);
            dialogOneButtonText = (TextView) findViewById(R.id.dialogOneButtonText);
            dialogOneButtonOkButton = (Button) findViewById(R.id.dialogOneButtonOkButton);
    
            dialogOneButtonOkButton.setOnClickListener(this);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            dialogOneButtonTitle.setText(dialogFields.getTitle());
            dialogOneButtonText.setText(dialogFields.getText());
            dialogOneButtonOkButton.setText(dialogFields.getOneButton());
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialogOneButtonOkButton:
    
                    break;
                default:
                    break;
            }
            dismiss();
        }
    }
    

    【讨论】:

      【解决方案7】:

      设置

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

      将阻止对话框投射阴影。

      解决方法是使用

      dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_rounded_background);
      

      R.drawable.dialog_rounded_background 在哪里

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          <item>
              <shape android:shape="rectangle" android:padding="10dp">
                  <solid
                      android:color="@color/dialog_bg_color"/>
                  <corners
                      android:radius="30dp" />
              </shape>
          </item>
      
      </layer-list>
      

      【讨论】:

      • 最优雅、最简单的解决方案,适用于 API 27、Android 8.1 Oreo。
      • 如果您仍然坚持使用 Theme.AppCompat,这是 2020 年的正确答案
      • 我不明白为什么这不是公认的答案...
      • 完美运行。这应该是公认的答案
      【解决方案8】:

      我做了一个没有背景可绘制的新方法是让它具有 CardView 作为父级并给它一个 app:cardCornerRadius="20dp" 然后将它添加到 java 类 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

      这是另一种制作方法。

      【讨论】:

        【解决方案9】:

        您可以简单地使用 MaterialAlertDialogBu​​ilder 创建带有圆角的自定义对话框。

        首先为材质对话框创建一个样式,如下所示:

        <style name="MyRounded.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
            <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.App.CustomDialog.Rounded
            </item>
            <item name="colorSurface">@color/YOUR_COLOR</item>
        </style>
        
        <style name="ShapeAppearanceOverlay.App.CustomDialog.Rounded" parent="">
            <item name="cornerFamily">rounded</item>
            <item name="cornerSize">10dp</item>
        </style>
        

        然后在 Java 类中创建一个 Alert Dialog 对象,如下所示:

        AlertDialog alertDialog =  new MaterialAlertDialogBuilder(this,R.style.MyRounded_MaterialComponents_MaterialAlertDialog)  // for fragment you can use getActivity() instead of this 
                            .setView(R.layout.custom_layout) // custom layout is here 
                            .show();
        
                    final EditText editText = alertDialog.findViewById(R.id.custom_layout_text);   // access to text view of custom layout         
                    Button btn = alertDialog.findViewById(R.id.custom_layout_btn);
        
                    btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
        
                            Log.d(TAG, "onClick: " + editText.getText().toString());
                        }
                    });
        

        这就是你需要做的。

        【讨论】:

        • 最正确的方式,别忘了用MaterialAlertDialogBu​​ilder代替AlertDialog.Builder
        • 那是很棒的坦克
        • 所有其他答案都已过时且模棱两可,但这是最简单,最准确的答案!谢谢
        【解决方案10】:

        您可以将形状用作背景-

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
        <corners android:radius="10dp" />
        <padding android:left="10dp" android:right="10dp"/>
        </shape>
        

        详情请关注this

        【讨论】:

          【解决方案11】:

          对于任何喜欢用 XML 做事的人,特别是在您使用导航架构组件操作来导航到对话框的情况下

          你可以使用:

          <style name="DialogStyle" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
          
              <!-- dialog_background is drawable shape with corner radius -->
              <item name="android:background">@drawable/dialog_background</item>
          
              <item name="android:windowBackground">@android:color/transparent</item>
          </style>
          

          【讨论】:

            【解决方案12】:

            最简单的方法是使用 from

            CardView 及其卡片:cardCornerRadius

            <?xml version="1.0" encoding="utf-8"?>
            <android.support.v7.widget.CardView 
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:card="http://schemas.android.com/apk/res-auto"
             android:orientation="vertical"
             android:id="@+id/cardlist_item"
             android:layout_width="match_parent"
             android:layout_height="130dp"
             card:cardCornerRadius="40dp"
             android:layout_marginLeft="8dp"
             android:layout_marginRight="8dp"
             android:layout_marginTop="5dp"
             android:layout_marginBottom="5dp"
             android:background="@color/white">
            
              <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="12sp"
                android:orientation="vertical"
                android:weightSum="1">
                </RelativeLayout>
            
            </android.support.v7.widget.CardView>
            

            当你创建你的对话框时

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

            【讨论】:

            • 此解决方案不允许 CardView 高程阴影完全显示
            【解决方案13】:

            如果您想控制对话框的角半径并保留高度阴影,这里是完整的解决方案

            对话框:

            class OptionsDialog: DialogFragment() {
            
            override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View {
                dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                return inflater.inflate(R.layout.dialog_options, container)
            }
            
            }
            

            dialog_options.xml 布局:

            <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            
            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="40dp"
                app:cardElevation="20dp"
                app:cardCornerRadius="12dp">
            
                <androidx.constraintlayout.widget.ConstraintLayout
                    id="@+id/actual_content_goes_here"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                </androidx.constraintlayout.widget.ConstraintLayout>
            
            </androidx.cardview.widget.CardView>
            </FrameLayout>
            

            关键是用另一个ViewGroup(这里是FrameLayout)包裹CardView,并设置margin为高程阴影创造空间。

            【讨论】:

              【解决方案14】:

              对于 API 级别 >= 28 可用属性 android:dialogCornerRadius 。要支持以前的 API 版本需要使用

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

              在哪里 dialog_bg.xml

              <?xml version="1.0" encoding="utf-8"?>
              <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
                  <item >
                      <shape >
                          <solid android:color="@android:color/transparent" />
                      </shape>
                  </item>
                  <item
                      android:left="16dp"
                      android:right="16dp">
                      <shape>
                          <solid
                              android:color="@color/white"/>
                          <corners
                              android:radius="8dp" />
              
                          <padding
                              android:left="16dp"
                              android:right="16dp" />
                      </shape>
                  </item>
              </layer-list>
              

              【讨论】:

              • 图层列表/透明形状是什么?
              【解决方案15】:

              在 Kotlin 中,我使用了一个类 DoubleButtonDialog.Java,其中 window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) 行是重要的

              class DoubleButtonDialog(context: Context) : Dialog(context, R.style.DialogTheme) {
              
                  private var cancelableDialog: Boolean = true
                  private var titleDialog: String? = null
                  private var messageDialog: String? = null
                  private var leftButtonDialog: String = "Yes"
                  //    private var rightButtonDialog: String? = null
                  private var onClickListenerDialog: OnClickListener? = null
              
                  override fun onCreate(savedInstanceState: Bundle?) {
                      window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                      //requestWindowFeature(android.view.Window.FEATURE_NO_TITLE)
                      setCancelable(cancelableDialog)
                      setContentView(R.layout.dialog_double_button)
              //        val btnNegative = findViewById<Button>(R.id.btnNegative)
              //        btnNegative.visibility = View.GONE
              //        if (rightButtonDialog != null) {
              //            btnNegative.visibility = View.VISIBLE
              //            btnNegative.text = rightButtonDialog
              //            btnNegative.setOnClickListener {
              //                dismiss()
              //                onClickListenerDialog?.onClickCancel()
              //            }
              //        }
                      val btnPositive = findViewById<Button>(R.id.btnPositive)
                      btnPositive.text = leftButtonDialog
                      btnPositive.setOnClickListener {
                          onClickListenerDialog?.onClick()
                          dismiss()
                      }
                      (findViewById<TextView>(R.id.title)).text = titleDialog
                      (findViewById<TextView>(R.id.message)).text = messageDialog
                      super.onCreate(savedInstanceState)
                  }
              
                  constructor(
                      context: Context, cancelableDialog: Boolean, titleDialog: String?,
                      messageDialog: String, leftButtonDialog: String, /*rightButtonDialog: String?,*/
                      onClickListenerDialog: OnClickListener
                  ) : this(context) {
                      this.cancelableDialog = cancelableDialog
                      this.titleDialog = titleDialog
                      this.messageDialog = messageDialog
                      this.leftButtonDialog = leftButtonDialog
              //        this.rightButtonDialog = rightButtonDialog
                      this.onClickListenerDialog = onClickListenerDialog
                  }
              }
              
              
              interface OnClickListener {
                  //    fun onClickCancel()
                  fun onClick()
              }
              

              在布局中,我们可以创建一个dialog_double_button.xml

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:tools="http://schemas.android.com/tools"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:layout_margin="@dimen/dimen_10"
                      android:background="@drawable/bg_double_button"
                      android:orientation="vertical"
                      android:padding="@dimen/dimen_5">
              
                      <TextView
                          android:id="@+id/title"
                          style="@style/TextViewStyle"
                          android:layout_gravity="center_horizontal"
                          android:layout_margin="@dimen/dimen_10"
                          android:fontFamily="@font/campton_semi_bold"
                          android:textColor="@color/red_dark4"
                          android:textSize="@dimen/text_size_24"
                          tools:text="@string/dial" />
              
                      <TextView
                          android:id="@+id/message"
                          style="@style/TextViewStyle"
                          android:layout_gravity="center_horizontal"
                          android:layout_margin="@dimen/dimen_10"
                          android:gravity="center"
                          android:textColor="@color/semi_gray_2"
                          tools:text="@string/diling_police_number" />
              
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_marginTop="@dimen/dimen_10"
                          android:gravity="center"
                      android:orientation="horizontal"
                      android:padding="@dimen/dimen_5">
              
                      <!--<Button
                          android:id="@+id/btnNegative"
                          style="@style/ButtonStyle"
                          android:layout_width="0dp"
                          android:layout_height="@dimen/dimen_40"
                          android:layout_marginEnd="@dimen/dimen_10"
                          android:layout_weight=".4"
                          android:text="@string/cancel" />-->
              
                      <Button
                          android:id="@+id/btnPositive"
                          style="@style/ButtonStyle"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:backgroundTint="@color/red_dark4"
                          android:fontFamily="@font/campton_semi_bold"
                          android:padding="@dimen/dimen_10"
                          android:text="@string/proceed"
                          android:textAllCaps="false"
                          android:textColor="@color/white"
                          android:textSize="@dimen/text_size_20" />
                  </LinearLayout>
              </LinearLayout>
              

              然后使用 drawable.xml 作为

              <?xml version="1.0" encoding="utf-8"?>
              <shape xmlns:android="http://schemas.android.com/apk/res/android">
                  <solid
                      android:color="@color/white"/>
                  <corners
                      android:radius="@dimen/dimen_10" />
                  <padding
                      android:left="@dimen/dimen_10"
                      android:top="@dimen/dimen_10"
                      android:right="@dimen/dimen_10"
                      android:bottom="@dimen/dimen_10" />
              </shape>
              

              【讨论】:

                【解决方案16】:

                在drawable中创建一个xml,比如customd.xml。

                然后在您的自定义对话框布局 xml 中将其设置为背景:

                android:background="@drawable/customd"
                

                最后在自定义Dialog类的java部分你需要这样做:

                public class Customdialoque extends DialogFragment {
                
                public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                
                    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                
                    View view = inflater.inflate(R.layout.activity_customdialoque, container, false);
                
                    return view;
                }
                

                【讨论】:

                  【解决方案17】:

                  这是一个基本的解决方案:

                   <style name="Style_Dialog_Rounded_Corner" parent="Theme.AppCompat.Light.Dialog.Alert">
                          <item name="android:windowBackground">@drawable/dialog_rounded_corner</item>
                          <item name="android:windowMinWidthMinor">85%</item>
                      </style>
                  

                  在 Drawable 中创建形状:

                  <?xml version="1.0" encoding="utf-8"?>
                  <shape xmlns:android="http://schemas.android.com/apk/res/android">
                      <solid android:color="#FFFFFF" />
                      <corners android:radius="12dp" />
                  </shape>
                  

                  Builder 构造器中的传递样式

                  AlertDialog alert = new AlertDialog.Builder(MainActivity.this,R.style.Style_Dialog_Rounded_Corner).create();
                  

                  【讨论】:

                    【解决方案18】:

                    我在自定义布局中使用 CardView 实现了圆角对话框并设置了圆角半径。

                    这是我的 xml 代码。

                    <?xml version="1.0" encoding="utf-8"?>
                    <androidx.cardview.widget.CardView 
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/bottomSheet"
                    android:layout_width="match_parent"
                    android:layout_margin="@dimen/padding_5dp"
                    app:cardCornerRadius="@dimen/dimen_20dp"
                    android:layout_height="wrap_content">
                    
                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/main_gradient_bg"
                        android:paddingBottom="32dp">
                    
                        <TextView
                            android:id="@+id/subdomain_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/margin_32dp"
                            android:layout_marginLeft="@dimen/margin_32dp"
                            android:layout_marginTop="@dimen/margin_50dp"
                            android:fontFamily="@font/nunito_sans"
                            android:text="@string/enter_subdomain"
                            android:textColor="@color/white"
                            android:textSize="@dimen/size_18sp"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
                    
                    
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/dimen_45dp"
                            app:layout_constraintLeft_toRightOf="@id/subdomain_label"
                            app:layout_constraintTop_toTopOf="parent"
                            app:srcCompat="@drawable/ic_baseline_info_24" />
                    
                        <EditText
                            android:id="@+id/subdomain_edit_text_bottom_sheet"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/dimen_20dp"
                            android:layout_marginLeft="@dimen/dimen_20dp"
                            android:layout_marginEnd="@dimen/dimen_20dp"
                            android:layout_marginRight="@dimen/dimen_20dp"
                            android:textColor="@color/white"
                            android:theme="@style/EditTextTheme"
                            app:layout_constraintTop_toBottomOf="@id/subdomain_label" />
                    
                        <Button
                            android:id="@+id/proceed_btn"
                            android:layout_width="@dimen/dimen_150dp"
                            android:layout_height="@dimen/margin_50dp"
                            android:layout_marginTop="@dimen/margin_30dp"
                            android:background="@drawable/primary_btn_bg"
                            android:text="@string/proceed"
                            android:textAllCaps="false"
                            android:textColor="@color/white"
                            android:textSize="@dimen/size_18sp"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/subdomain_edit_text_bottom_sheet" />
                    </androidx.constraintlayout.widget.ConstraintLayout>
                    </androidx.cardview.widget.CardView>
                    

                    之后,我在 Kotlin 中这样称呼它:-

                        val builder = AlertDialog.Builder(mContext)
                        val viewGroup: ViewGroup = findViewById(android.R.id.content)
                        val dialogView: View = 
                        LayoutInflater.from(mContext).inflate(R.layout.subdomain_bottom_sheet, 
                        viewGroup, false)
                        val alertDialog: AlertDialog = builder.create()
                        alertDialog.setView(dialogView,0,0,0,0)
                        alertDialog.show()
                        val windowParam = WindowManager.LayoutParams()
                        windowParam.copyFrom(alertDialog.window!!.attributes)
                        windowParam.width = AppConstant.getDisplayMetricsWidth(mContext) - 100
                        windowParam.height = WindowManager.LayoutParams.WRAP_CONTENT
                        windowParam.gravity = Gravity.CENTER
                        alertDialog.window!!.attributes = windowParam
                        
                       
                        alertDialog.window!!.setBackgroundDrawable
                        (ColorDrawable(Color.TRANSPARENT))
                    

                    最后一行非常重要。缺少它会导致颜色(主要是白色)显示在角落后面。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2016-07-18
                      • 2014-03-13
                      • 2020-10-23
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-09-27
                      相关资源
                      最近更新 更多