【问题标题】:Unable to remove dialog background无法删除对话框背景
【发布时间】:2021-12-19 09:47:11
【问题描述】:

您好,我正在尝试删除对话框的背景

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

效果很好

但在为我的对话框实现阴影效果后,即使背景可绘制设置为透明,此问题也会再次出现

这是我添加的行

getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);

这是它的screenshot

完整代码

sort_image_dialog_fragment.java

 public class sort_image_dialoge_fragment extends DialogFragment {
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.sort_image, container, false);
        view.setBackgroundResource(R.drawable.dialog_rounded_background);

        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            getDialog().getWindow().setDimAmount(0.8f);
            getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        }
        return view;
    }

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

sort_image.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:backgroundTint="@color/grey"
        android:elevation="12dp"
        app:cardCornerRadius="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/sort_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerInParent="true"
                android:layout_marginTop="5dp"
                android:fontFamily="@font/roboto"
                android:text="Sort by"
                android:textAlignment="center"
                android:textColor="@color/lite_grey"
                android:textSize="25sp" />

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/divider_line_sort_by"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@id/sort_textView"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="10dp"
                android:backgroundTint="@color/lite_grey"
                app:cardCornerRadius="50dp" />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true">

                <TextView
                    android:id="@+id/latest_sort_textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30dp"
                    android:layout_marginTop="30dp"
                    android:fontFamily="@font/roboto"
                    android:text="Latest"
                    android:textAlignment="center"
                    android:textColor="@color/lite_grey"
                    android:textSize="25sp" />

                <TextView
                    android:id="@+id/most_popular_sort_textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/latest_sort_textView"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_marginStart="30dp"
                    android:layout_marginTop="25sp"
                    android:fontFamily="@font/roboto"
                    android:text="Most Popular"
                    android:textAlignment="center"
                    android:textColor="@color/lite_grey"
                    android:textSize="25sp" />

                <RadioGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true">

                    <RadioButton
                        android:id="@+id/latest_sort_radio_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="23dp" />

                    <RadioButton
                        android:id="@+id/most_popular_sort_radio_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:checked="true" />

                </RadioGroup>

            </RelativeLayout>


        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-alertdialog android-dialogfragment customdialog


    【解决方案1】:

    问题

    该问题与布局无关,因为您分配给对话框的任何布局;对话框将其应用于其方形窗口;并且对话窗口默认为白色矩形(即没有圆角)。 默认情况下,此矩形布局的外部部分是灰色的。

    因此,您在角落看到的白色与对话框窗口背景有关。

    解决方案

    所以,为了制作一个圆角对话框;您必须通过以下 2 个步骤在其窗口上更改它:

    • 第 1 步: 在themes.xml/styles.xml 中创建一个删除对话框窗口背景的主题:
        <style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
            <item name="android:windowBackground">@null</item>
        </style>
    

    通过在您的自定义 DialogFragment 类中覆盖 getTheme() 来应用此主题:

    @Override
    public int getTheme() {
        return R.style.NoBackgroundDialogTheme;
    }
    
    • 第 2 步::创建一个与您的卡片半径 (20dp) 完全一致的圆形可绘制对象:

    res\drawable\rounded_background.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="20dp" />
    </shape>
    

    使用view.setBackgroundResource() 将此应用到dialogFragment 视图并删除您在onCreateView() 中提供的其他窗口调用:

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.sort_image, container, false);
        view.setBackgroundResource(R.drawable.rounded_background);
        return view;
    }
    

    更新:

    • 确保使用小写的@null 而不是@Null。
    • sort_image_dialog_fragment.java 现在应该如下所示:
    public class sort_image_dialog_fragment extends DialogFragment {
    
        @Nullable
        @org.jetbrains.annotations.Nullable
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            View view = inflater.inflate(R.layout.sort_image, container, false);
            view.setBackgroundResource(R.drawable.rounded_background);
            return view;
        }
    
        @Override
        public int getTheme() {
            return R.style.NoBackgroundDialogTheme;
        }
    
    
    }
    

    结果:

    旁注:类的命名约定应以大写开头,并遵循驼峰式模式;所以sort_image_dialog_fragment 将改为SortImageDialgFragment。

    【讨论】:

    • 谢谢你的回答,好的我明白所有的步骤只是对第一步有疑问你能告诉我我到底要做什么我不太了解第一步
    • 好的,我已经尝试过您的解决方案,但是整个布局显示的问题而不仅仅是卡片视图,好的,我想清除在您的第一步中我感到困惑的事情,所以可能我第一步搞砸了,这就是为什么会出现这种情况
    • 请将RelativeLayout中的match_parent改为wrap_content...
    • 第一步..您需要更改对话框的默认样式,以便通过将其设置为空来删除背景。所以你打开styles.xml或themes.xml文件在答案中添加新样式...然后覆盖你的sort_image_dialog_fragment.java类中的getTheme方法以包含新样式
    • 我添加了这样的样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2019-10-27
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多