android - CardView角落显示白色像素?

标签 android android-layout android-cardview

我正在尝试创建一个自定义的“警告”对话框,我希望它有圆角。因此,我将整个布局放入 CardView 中,并将对话框的布局设置为此布局。
但是,在深色背景上,例如默认的 Android 对话框背景,我的对话框的角落似乎出现了白色像素。
我已经尝试过建议的 here , 设置 app:cardBackgroundColor="@color/transparent" ,但这并没有什么不同。
我的对话框的布局如下(它与屏幕截图不同,但按钮、bakcground 和 CardView 是相同的):

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/error_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/error_dialog_margin"
    android:layout_marginEnd="@dimen/error_dialog_margin"
    app:cardCornerRadius="@dimen/error_dialog_corner_radius"
    app:cardElevation="@dimen/error_dialog_elevation"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true">

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

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/error_icon"
            style="@style/ErrorViewTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_outer_margin"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/error_icon"
            android:tint="@color/redColor" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/error_title"
            style="@style/ErrorViewTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/default_outer_margin"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginEnd="@dimen/default_outer_margin"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:maxLines="2"
            android:text="@string/something_went_wrong" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/error_message"
            style="@style/ErrorViewMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/default_outer_margin"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginEnd="@dimen/default_outer_margin"
            android:gravity="center"
            android:minHeight="55dp"
            tools:text="@string/try_again_later" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_margin"
            android:background="@color/blueColor">

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/error_ok_button"
                style="@style/PrimaryButton"
                android:layout_width="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                android:text="@string/ok_button" />

        </FrameLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>
请检查问题的屏幕截图:
enter image description here
enter image description here
知道为什么会发生这种情况以及如何解决吗?它似乎与cardview的背景有关,因为将所有背景设置为透明可以解决问题,但我需要对话框背景为白色。

最佳答案

对我来说设置

app:cardBackgroundColor="@color/transparent"
在 CardView 中修复了它

关于android - CardView角落显示白色像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65147250/

相关文章:

android - addView 将文本推送到抽屉布局下方

android - 创建 GoogleApiClient 实例时出现 NullPointerException

android - 为什么每当我发送模拟 GPS 信号时模拟器就会崩溃?

android - 在 RecyclerView 中将 CardView 居中,只有一个元素

android - Cardview 在低于 Lollipop 的设备中无法正常工作

java - 如何检测 ondraglistener 是否已经完成? (安卓)

android - 当 PagerAdapter.getPageWidth() < 1f 时,使所有 ViewPager 项目居中对齐

java - 带有标题部分的Android自定义 ListView 多行

android - 如何更改 EditText 上的图标?

android - CardView的优缺点