我有一个自定义的 CardView 支持小部件实现,但当我将其包含在布局文件中时,似乎无法使圆角背景透明。然而,如果我在布局文件中简单地放置 CardView 支持小部件,则它突然起作用了。如何让我自定义的组件的圆角透明呢?
这是我自定义的 CardView 的布局文件:
view_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Custom.Widget.CardView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/default_padding">
<TextView
android:id="@+id/view_mainText"
style="@style/Custom.Widget.TextView.Header"
android:textColor="@color/instruction_balloon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view_subText"
android:textSize="@dimen/text_size_medium"
android:textColor="@color/instruction_balloon_text"
android:singleLine="false"
android:text="Please remove white corners :-("
android:textIsSelectable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
样式.xml
<style name="Custom.Widget.CardView" parent="CardView">
<item name="cardBackgroundColor">@color/card_backgroundColor</item>
<item name="cardCornerRadius">12dp</item>
</style>
这是我的布局文件,包含了两个CardView。第一个带有白色角落,第二个基本上与view_card.xml相同的布局,但没有白色角落(透明)。
example.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<some.private.namespace.CardView
android:id="@+id/custom_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin" />
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin"
style="@style/Custom.Widget.CardView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/default_padding">
<TextView
android:id="@+id/view_mainText"
style="@style/Custom.Widget.TextView.Header"
android:textColor="@color/instruction_balloon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/view_subText"
android:textSize="@dimen/text_size_medium"
android:textColor="@color/instruction_balloon_text"
android:singleLine="false"
android:text="I have no white corners :-)"
android:textIsSelectable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
... some other views
</LinearLayout>
更新1
我尝试了Just89提供的解决方案,但在较低版本的安卓系统上会导致崩溃。
android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
经过快速搜索,我找到了下面的帖子:android.graphics.drawable.ColorDrawable不能转换为android.support.v7.widget.RoundRectDrawableWithShadow
答案建议使用setCardBackgroundColor
来设置背景颜色。但是这会导致边角出现白色。
更新2
已被接受的答案将解决此问题,但这不是首选解决方案。在创建自定义CardView组件时,我犯了一个错误,导致出现了这些白色边角。请查看此答案以了解我犯了什么错误。