CardView的透明背景 - 安卓

109

我想在CardView上实现透明背景。 我知道backgroundColor,但是我的布局上有图片。

你知道如何做吗?或者有什么可以像cardview一样工作,但我可以设置透明背景的东西吗?

问候


你尝试过使用 android:background="@android:color/transparent" 吗? - Psypher
7
你应该使用 cardBackgroundColor 吗? - harism
1
android:background="@android:color/transparent" 不起作用 我不使用backgroundColor,因为它没有透明选项。 - mac229
1
我曾经遇到过同样的问题,无法弄清如何使其透明。 - Tyler Pfaff
8个回答

215

使用cardBackgroundColor属性来去除颜色,使用cardElevation属性来去除下降阴影。例如:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myCardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardElevation="0dp"> 

要查看支持的属性列表,请参见此处:https://developer.android.com/reference/android/support/v7/widget/CardView.html

如果您使用的是较旧的API,您需要在您的CardView上调用这两个函数:

myCardView.setCardBackgroundColor(Color.TRANSPARENT);
myCardView.setCardElevation(0);

嗨,它可以工作,你能告诉我android:和card_view:命名空间之间的区别吗? - user3290180
它在API 19上无法工作,但在API 21上可以。如何使其适用于较低的API? - Muneeb Mirza
2
@MuneebMirza 在你的代码中调用 setCardElevation()setCardBackgroundColor() 方法来设置你的 CardView。请查看我的修改。 - Chris Stillwell
1
好的,我尝试了这个答案,它有效 :) https://dev59.com/KVsW5IYBdhLWcg3wk4HM?rq=1 - Muneeb Mirza
我尝试设置@null但没有成功,有什么想法吗? - Gokhan Arik
显示剩余4条评论

20

在SDK版本21或更高版本中,使Android CardView透明的步骤。

  1. 设置 android:backgroundTint="@android:color/transparent"。这是用于设置背景的CardView属性。

  2. 设置android:cardElevation="0dp"以删除阴影。

例如,以下是创建透明CardView的小xml代码:

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardElevation="0dp" />

8

我使用的是 属性 android:backgroundTint="@color/some_color",仅适用于API级别21及以上。例如,颜色为#50000000

<android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="3dp"
        app:cardElevation="0dp"
        android:backgroundTint="@color/negro_label"
        >


4

使用 app:cardBackgroundColor="@android:color/transparent"


<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    app:cardCornerRadius="16dp"
    app:cardElevation="16dp"
    app:cardBackgroundColor="@android:color/transparent" >

<--inside cardlayout-->

    </android.support.v7.widget.CardView>

3

这应该可在API 17上运行。

cardView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));

1

只需添加背景颜色 app:cardBackgroundColor="#0000"

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardBackgroundColor="#0000"> 

0

你必须拥有 cardView 才能使图像呈圆形

       <androidx.cardview.widget.CardView
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:cardCornerRadius="25dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:elevation="0dp"
            app:cardBackgroundColor="#00424242"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/play_icon"
                android:padding="5dp"
                android:background="#19000000"
                android:contentDescription="TODO" />

        </androidx.cardview.widget.CardView>

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
我想要的是在卡片视图内部放置一个文本视图,但我希望卡片视图的背面是透明的。我通过以下方式解决了这个问题:
    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:cardBackgroundColor="@color/semiTransparentColorr"
        app:cardUseCompatPadding="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/cardview_stroke"
            android:padding="16dp">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Gold"
                android:textSize="12sp" />


        </LinearLayout>
    </androidx.cardview.widget.CardView>

首先,您需要将cardview的背景颜色值设置为#1AFFFFFF(半透明白色)。然后我们将cardview_stroke.xml作为linearlayout的背景。cardview_stroke.xml的代码应该是这样的:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/semiTransparentColorr"/>
    <stroke android:width="1dp" android:color="@color/black"></stroke>
    <corners android:radius="8dp"/>
</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接