自定义 AlertDialog 的圆角

Rounded corners for custom AlertDialog

我一直在将我的应用程序忘记密码功能从 Activity 更新为简单的自定义警报对话框。我应该如何为 Dialog window 添加圆角?我已经阅读了多个教程,但 none 似乎适用于我的情况。我从某个地方读到我应该在某个地方使用 setBackgroundResources 方法,但我不确定在哪里。

Kotlin 代码

// Forgot password textview onClick Listener
    binding.tvForgotPassword.setOnClickListener {

        // Inflate add_item_dialog.xml custom view
        val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_forgot_password, null)

        // Add AlertDialog Builder
        val dialogBuilder = AlertDialog.Builder(this)
            .setView(dialogView)

        // Binding add_item_dialog layout
        val dialogBinding = DialogForgotPasswordBinding.bind(dialogView)

        // Show Forgot password Dialog
        val customAlertDialog = dialogBuilder.show()
        dialogBinding.ivCloseDialog.setOnClickListener {
            customAlertDialog.dismiss()
        }
    }

dialog_forgot_password.xml

<androidx.cardview.widget.CardView
    android:layout_width="350dp"
    android:layout_height="500dp"
    app:cardCornerRadius="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="350dp"
        android:layout_height="500dp">


        <ImageView
            android:id="@+id/iv_closeDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.94"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.060000002"
            app:srcCompat="@drawable/ic_close" />

        <TextView
            android:id="@+id/tv_forgotPasswordInfo"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:textSize="18sp"
            android:text="@string/enter_email"
            android:textColor="@color/ColorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_forgotPassword" />

        <Button
            android:id="@+id/submitBtn"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/button"
            android:elevation="4dp"
            android:text="@string/submit"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/emailForgot"
            app:layout_constraintVertical_bias="1.0" />

        <TextView
            android:id="@+id/tv_forgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="36dp"
            android:fontFamily="sans-serif-black"
            android:text="@string/forgot_password"
            android:textColor="@color/ColorPrimary"
            android:textSize="40sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/iv_closeDialog" />

        <EditText
            android:id="@+id/emailForgot"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="@drawable/rounded_edittext"
            android:ems="10"
            android:hint="@string/email_address"
            android:importantForAutofill="no"
            android:inputType="textEmailAddress"
            android:paddingStart="15dp"
            android:textColorHint="@color/ColorPrimary"
            app:layout_constraintBottom_toTopOf="@+id/submitBtn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_forgotPasswordInfo"
            app:layout_constraintVertical_bias="1.0" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

Rounded_framelayout.xml

<solid android:color="#FFFFFF"/>


<padding android:left="2dp"
    android:top="2dp"
    android:right="2dp"
    android:bottom="2dp"/>

<corners android:radius="20dp" />

TRY THIS:

class your_kotlin_filename :DialogFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    dialog!!.window?.setBackgroundDrawableResource(R.drawable.Rounded_framelayout);
    return inflater.inflate(R.layout.dialog_forgot_password, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
............your code for forget password...........
}



 override fun onStart() {
    super.onStart()
    val width = (resources.displayMetrics.widthPixels * 0.95).toInt()
    val height = (resources.displayMetrics.heightPixels * 0.43).toInt()
    dialog!!.window?.setLayout(width, height)
    dialog!!.setCanceledOnTouchOutside(true)
}

请查看 this 指南以了解如何自定义对话框。

简而言之,您需要像平常一样创建对话框,但在显示之前设置背景资源:

val builder = AlertDialog.Builder(context)
builder.setView(R.layout.item_dialog)
val dialog = builder.create()
dialog.window?.decorView?.setBackgroundResource(R.drawable.dialog_background) // setting the background
dialog.show()

其中 dialog_background 的定义如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="16dp" />
    <solid android:color="?android:colorBackground" />
</shape>