我有一个CardView支持小部件的自定义实现,但是当我将其包含在布局文件中时,似乎无法使角的背景透明.但是,如果我只是将CardView支持小部件放在布局文件中,则它会突然起作用.如何使自定义组件的角部透明?
I have a custom implementation of a CardView support widget but I can't seem to get the background for the corners transparent when I include it in my layout-file. However, if I simply place the CardView support widget in my layout-file it suddenly works. How can I get the corners transparent for my custom component?
这是CardView的自定义实现的布局文件:
This is the layout file for my custom implementation of the CardView:
view_card.xml
view_card.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="schemas.android/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>
styles.xml
styles.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基本上具有相同的布局,但没有白色角(透明).
And this is my layout file that includes the two CardViews. The first one with the white corners and the second one that's basically the same layout as view_card.xml but without the white corners (transparent).
example.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="schemas.android/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版本上导致崩溃.
I tried Just89's solution, however it results in a crash on lower Android versions.
android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow快速搜索后,我发现了以下信息. android.graphics.drawable.ColorDrawable无法投射到android.support.v7.widget.RoundRectDrawableWithShadow
After a quick search I found the following post. android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
答案建议使用以下方法设置背景颜色:setCardBackgroundColor. 但是,这将带回白角.
The answer suggests to set the background color using:setCardBackgroundColor. However this will bring back the white corners.
更新2
可接受的答案将解决此问题,但这不是首选的解决方案.创建导致这些白角的自定义CardView组件时,我犯了一个错误.检查此答案,看看我做错了什么.
The accepted answer will solve this problem, however it's not the preferred solution. I made a mistake when creating the custom CardView component that was causing these white corners. Check this answer to see what I did wrong.
推荐答案在自定义实现中的可用上下文上使用以下代码:
Use the following code, on a place where the context is available, in your custom implementation:
setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));对于低于Lollipop的android版本,请使用以下代码,以避免上述崩溃.
Use the following code for android versions lower than Lollipop to avoid the mentioned crash.
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){ getBackground().setAlpha(0); } else { setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent); }更多推荐
Cardview角落背景不透明
发布评论