我有一个CardView支持小部件的自定义实现,但是当我将它包含在我的布局文件中时,我似乎无法获得角落的背景透明.但是,如果我只是将CardView支持小部件放在我的布局文件中,它就会突然运行.如何让自定义组件的角落透明?
这是我自定义CardView实现的布局文件:
view_card.xml
android:id="@+id/view_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Custom.Widget.CardView">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/default_padding">
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" />
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" />
styles.xml
@color/card_backgroundColor
12dp
这是我的布局文件,包括两个CardViews.第一个带有白色角落,第二个带有与view_card.xml基本相同的布局但没有白色角落(透明).
的example.xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/custom_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin" />
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">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/default_padding">
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" />
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" />
... some other views
更新1
我尝试过Just89的解决方案,但是它导致较低的Android版本崩溃.
android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
答案建议使用:setCardBackgroundColor设置背景颜色.
然而,这将带回白色角落.
更新2
接受的答案将解决这个问题,但它不是首选的解决方案.我在创建导致这些白色角落的自定义CardView组件时犯了一个错误.检查this答案,看看我做错了什么.