首页 > 解决方案 > 在android中删除圆角外的自定义对话框颜色

问题描述

我正在尝试将圆角应用于我的自定义对话框。对话框正确显示圆角,但角的外部空间变为白色。

可绘制/dialog_message_background

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="?postCardBackground"/> //light black color
    <corners
        android:radius="30dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    android:background="@drawable/dialog_message_background"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:background="@drawable/dialog_message_background"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_width="250dp"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/dialogMessageTv"
                android:text="Error occured"
                android:maxLines="3"
                android:textStyle="bold"
                android:textColor="?attr/postUserNameColor"
                android:textSize="18sp"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        

        <ImageView
            android:layout_marginTop="20dp"
            android:id="@+id/messageDialogImg"
            android:layout_width="70dp"
            android:layout_height="70dp" />

        <Button
            android:id="@+id/messageDialogOkBtn"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/dark_purple_btn_back"
            android:text="ok"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@color/white"
            android:layout_width="150dp"
            android:layout_height="40dp" />
    </LinearLayout>
</RelativeLayout>

输出窗口

输出屏幕显示对话框外的白角

帮我去除自定义对话框外的白色。

标签: androidandroid-layoutdialogandroid-drawable

解决方案


您不需要形状来实现Dialog圆角。您可以覆盖您的getTheme()方法DialogFragment

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>

在此处输入图像描述

在 Java 中只需使用

public class RoundedDialog extends DialogFragment {

    @Override
    public int getTheme() {
        return   R.style.RoundedCornersDialog;
    }
}

推荐阅读