Cardview角落背景不透明

Wir*_*ing 10 android android-custom-view android-layout android-support-library android-cardview

我有一个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>
Run Code Online (Sandbox Code Playgroud)

styles.xml

<style name="Custom.Widget.CardView" parent="CardView">
    <item name="cardBackgroundColor">@color/card_backgroundColor</item>
    <item name="cardCornerRadius">12dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是我的布局文件,包括两个CardViews.第一个带有白色角落,第二个带有与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>
Run Code Online (Sandbox Code Playgroud)

更新1

我尝试过Just89的解决方案,但是它导致较低的Android版本崩溃.

 android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
Run Code Online (Sandbox Code Playgroud)

快速搜索后,我找到了以下帖子. android.graphics.drawable.ColorDrawable无法强制转换为android.support.v7.widget.RoundRectDrawableWithShadow

答案建议使用以下方式设置背景颜色:setCardBackgroundColor.然而,这将带回白色角落.

更新2

接受的答案将解决这个问题,但它不是首选的解决方案.在创建导致这些白色角落的自定义CardView组件时,我犯了一个错误.检查这个答案,看看我做错了什么.

小智 23

在自定义实现中,在上下文可用的位置使用以下代码:

setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
Run Code Online (Sandbox Code Playgroud)

编辑:

使用以下代码为Android版本低于Lollipop以避免上述崩溃.

  if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
     getBackground().setAlpha(0);
  } else {
     setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent);
  }
Run Code Online (Sandbox Code Playgroud)


Wir*_*ing 5

几年后,我得出的结论是,我在创建复合组件时犯了一个根本性错误。

当制作扩展 android.support.v7.widget.CardView 的复合组件时,膨胀的布局 xml 不应包含 CardView。基本上,您只需将一个 CardView 添加到另一个 CardView 中,这将导致我的自定义 CardView 后面出现白色角。

接受的答案将解决这个问题。但这并不是完美的解决方案。

有两种方法可以解决实际问题:

  1. 复合视图扩展了 android.support.v7.widget.CardView 并且布局 xml 不包含 android.support.v7.widget.CardView 而是包含 LinearLayout 作为根。造型必须在课堂上处理。
  2. 复合视图扩展了 LinearLayout,布局 xml 包含 android.support.v7.widget.CardView 作为根。

我选择第一个解决方案来解决这个问题。我希望这可以帮助那些犯过同样错误的人。