cardBackgroundColor 和 cardCornerRadius 在 AndroidX 中不起作用
cardBackgroundColor and cardCornerRadius not working in AndroidX
我正在为 AndroidX 库的 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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="@dimen/retail_card_width"
card_view:cardCornerRadius="@dimen/card_radius"
card_view:cardBackgroundColor="@color/baseYellow"
android:layout_height="@dimen/retail_card_height">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
tools:src="@drawable/ic_fruit_1"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:scaleType="fitEnd"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/ivRetailBrand"
android:layout_width="@dimen/brand_icon_size"
android:layout_height="@dimen/brand_icon_size"
tools:src="@drawable/esselunga"
android:layout_marginTop="@dimen/retail_brand_margin"
android:background="@drawable/round_outline"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="@dimen/retail_brand_margin"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
不幸的是,cardCornerRadius
和 cardBackgroundColor
似乎都不适合我的布局。我不明白我的问题是否取决于 AndroidX 库。
这是我的布局预览:
尝试将 CardView 修改为:
<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:layout_width="@dimen/retail_card_width"
app:cardCornerRadius="@dimen/card_radius"
app:cardBackgroundColor="@color/baseYellow"
android:layout_height="@dimen/retail_card_height">
我认为这与 CardView 设置无关。
检查你的 ImageView 它们可能是角不圆的原因。
注释掉图像查看代码(整个 ConstraintLayout)并检查 UI.As 我过去遇到过同样的问题。
原来问题出在我将 cardview androidx 库与支持的 recyclerview 库混合在一起。一旦我重建项目:
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
一切都很好。
我在应用级别 compileSdkVersion 从 28 升级到 29 gradle,问题解决了。
compileSdkVersion 29
我可能会迟到,但让我以后节省别人的时间。我在 Android Studio preview
上遇到了这个问题,后来意识到问题是 CardView 中的内容不是 clipped
到 CardView 的边界,所以你看到的角是 ImageView 的角,当您 运行 emulator/device.
上的应用程序时,它实际上被剪掉了
希望对您有所帮助!
我在清单文件中删除了这一行,之后我的卡片视图就可以正常工作了
android:hardwareAccelerated="false"
您可以创建一个新的可绘制文件 round_corner_colored.xml 并将以下代码粘贴到该文件中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/light_sky_blue"/>
<corners
android:radius="@dimen/_12sdp"/>
</shape>
现在将这个可绘制的卡片视图背景设置为:
android:background="@drawable/round_corner_colored"
希望对您有所帮助!!
尝试清理并重建项目。这对我有用..
我遇到了类似的问题。我通过将 CardView 包装在 FrameLayout 中解决了这个问题。
补充一个答案:
或者您可以尝试使用“app”命名空间而不是“card_view”:
app:cardBackgroundColor="@color/background_color"
app:cardCornerRadius="@dimen/corner_radius"
有些品牌会强制 Dark mode
首次安装应用程序时,这会影响视图的着色,并且在 CardView 中应用颜色会被忽略。您可以将此应用于您的主题以防止此类事件发生。
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
对我来说,它在预览中显示不正确。但它在设备上正确显示。存在一些渲染问题。我在android工作室一直遇到这样的问题。
在 Android Studio 4.2.1 (Mac)
中遇到问题
对我来说这很有效
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:contentPadding="@dimen/_10sdp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_10sdp">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/low"
android:textColor="@color/text_color_blue"
android:textSize="@dimen/_11ssp"
android:textStyle="normal" />
</androidx.cardview.widget.CardView>
我正在为 AndroidX 库的 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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="@dimen/retail_card_width"
card_view:cardCornerRadius="@dimen/card_radius"
card_view:cardBackgroundColor="@color/baseYellow"
android:layout_height="@dimen/retail_card_height">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
tools:src="@drawable/ic_fruit_1"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:scaleType="fitEnd"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/ivRetailBrand"
android:layout_width="@dimen/brand_icon_size"
android:layout_height="@dimen/brand_icon_size"
tools:src="@drawable/esselunga"
android:layout_marginTop="@dimen/retail_brand_margin"
android:background="@drawable/round_outline"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="@dimen/retail_brand_margin"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
不幸的是,cardCornerRadius
和 cardBackgroundColor
似乎都不适合我的布局。我不明白我的问题是否取决于 AndroidX 库。
这是我的布局预览:
尝试将 CardView 修改为:
<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:layout_width="@dimen/retail_card_width"
app:cardCornerRadius="@dimen/card_radius"
app:cardBackgroundColor="@color/baseYellow"
android:layout_height="@dimen/retail_card_height">
我认为这与 CardView 设置无关。
检查你的 ImageView 它们可能是角不圆的原因。
注释掉图像查看代码(整个 ConstraintLayout)并检查 UI.As 我过去遇到过同样的问题。
原来问题出在我将 cardview androidx 库与支持的 recyclerview 库混合在一起。一旦我重建项目:
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
一切都很好。
我在应用级别 compileSdkVersion 从 28 升级到 29 gradle,问题解决了。
compileSdkVersion 29
我可能会迟到,但让我以后节省别人的时间。我在 Android Studio preview
上遇到了这个问题,后来意识到问题是 CardView 中的内容不是 clipped
到 CardView 的边界,所以你看到的角是 ImageView 的角,当您 运行 emulator/device.
希望对您有所帮助!
我在清单文件中删除了这一行,之后我的卡片视图就可以正常工作了
android:hardwareAccelerated="false"
您可以创建一个新的可绘制文件 round_corner_colored.xml 并将以下代码粘贴到该文件中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/light_sky_blue"/>
<corners
android:radius="@dimen/_12sdp"/>
</shape>
现在将这个可绘制的卡片视图背景设置为:
android:background="@drawable/round_corner_colored"
希望对您有所帮助!!
尝试清理并重建项目。这对我有用..
我遇到了类似的问题。我通过将 CardView 包装在 FrameLayout 中解决了这个问题。
补充一个答案:
或者您可以尝试使用“app”命名空间而不是“card_view”:
app:cardBackgroundColor="@color/background_color"
app:cardCornerRadius="@dimen/corner_radius"
有些品牌会强制 Dark mode
首次安装应用程序时,这会影响视图的着色,并且在 CardView 中应用颜色会被忽略。您可以将此应用于您的主题以防止此类事件发生。
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
对我来说,它在预览中显示不正确。但它在设备上正确显示。存在一些渲染问题。我在android工作室一直遇到这样的问题。
在 Android Studio 4.2.1 (Mac)
中遇到问题对我来说这很有效
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:contentPadding="@dimen/_10sdp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_10sdp">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/low"
android:textColor="@color/text_color_blue"
android:textSize="@dimen/_11ssp"
android:textStyle="normal" />
</androidx.cardview.widget.CardView>