如何使我的Android对话框中的白色角透明?[副本]

rbl8hiat  于 12个月前  发布在  Android
关注(0)|答案(3)|浏览(98)

此问题已在此处有答案

How to make custom dialog with rounded corners in android(20个回答)
2天前关闭。
我有一个问题在我的弹出对话框。有白色角落作为背景。我认为这与相对布局有关,因为当我改变相对布局的背景颜色时,白色角也会改变。

我使用相对布局与此代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_background"
        android:orientation="vertical">

        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:background="@drawable/cafexo_icon"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingHorizontal="20dp"
            android:paddingBottom="20dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvPopupProductName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingVertical="12dp"
                android:textSize="20sp"
                android:textStyle="bold"
                android:text="PRODUCT NAME"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:textStyle="bold"
                android:textSize="16sp"
                android:text="DESCRIPTION"/>

            <TextView
                android:id="@+id/tvPopupProductDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:textSize="12sp"
                android:text="DESCRIPTION"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:textStyle="bold"
                android:textSize="20sp"
                android:layout_marginTop="20dp"
                android:text="Nutrition Facts"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingHorizontal="20dp"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:paddingVertical="4dp"
                        android:text="Amount per serving"
                        android:textSize="12sp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:layout_weight="1"
                            android:textStyle="bold"
                            android:text="Caffeine"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupCaffeineAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <com.google.android.material.divider.MaterialDivider
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:dividerColor="@color/black" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:textStyle="bold"
                            android:layout_weight="1"
                            android:text="Calories"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupCaloriesAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <com.google.android.material.divider.MaterialDivider
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:dividerColor="@color/black" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:textStyle="bold"
                            android:layout_weight="1"
                            android:text="Fat"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupFatAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <com.google.android.material.divider.MaterialDivider
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:dividerColor="@color/black" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:textStyle="bold"
                            android:layout_weight="1"
                            android:text="Carbs"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupCarbsAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <com.google.android.material.divider.MaterialDivider
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:dividerColor="@color/black" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:textStyle="bold"
                            android:layout_weight="1"
                            android:text="Protein"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupProteinAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <com.google.android.material.divider.MaterialDivider
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:dividerColor="@color/black" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:textStyle="bold"
                            android:layout_weight="1"
                            android:text="Sodium"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupSodiumAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <com.google.android.material.divider.MaterialDivider
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:dividerColor="@color/black" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="start"
                            android:textStyle="bold"
                            android:layout_weight="1"
                            android:text="Sugar"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/tvPopupSugarAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:layout_weight="1"
                            android:text="AMOUNT"
                            android:textSize="12sp" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

这里是我的圆角可绘制的xml

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

    <stroke android:width="3dp" android:color="@color/coffee"/>
    <gradient
        android:type="radial"
        android:startColor="#f8e5e0"
        android:centerColor="@color/accent"
        android:endColor="@color/background"
        android:centerX="0"
        android:centerY="0"
        android:gradientRadius="500dp"
        />
    <corners android:radius="25dp"/>

</shape>

Java代码:

popupDialog = new Dialog(getContext());
popupDialog.setContentView(R.layout.popup_product);
popupDialog.setCancelable(true);                          popupDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
popupDialog.show();

我该如何解决这个问题?我尝试popupDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

unftdfkk

unftdfkk1#

也许它必须对这个android做些什么:background="@drawable/circle_background”尝试改变那个背景的颜色

ergxz8rk

ergxz8rk2#

在设置内容视图之前,您需要设置背景透明

popupDialog = new Dialog(getContext());
popupDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
popupDialog.setContentView(R.layout.popup_product);
popupDialog.setCancelable(true);                          
popupDialog.show();
ssgvzors

ssgvzors3#

圆角可绘制xml中的渐变可能导致出现白色角。尝试将其替换为透明颜色:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent"/>
    <stroke android:width="3dp" android:color="@color/coffee"/>
    <corners android:radius="25dp"/>
</shape>

相关问题