CardView上的透明背景 - Android

时间:2015-02-20 12:57:06

标签: android background android-cardview

我想在CardView上做透明背景。 我知道backgroundColor,但我的布局上有图像。

你知道怎么做吗?或者作为cardview的东西,但我会设置一个透明的背景?

此致

6 个答案:

答案 0 :(得分:116)

设置您的CardView以使用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);

答案 1 :(得分:3)

简单的2个步骤,使Android CardView透明。

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

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

  3. 例如,这里有一个小的xml代码来创建透明的CardView

    <android.support.v7.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" />
    
      

    注意:不要使用 setBackground 。请改用app:cardBackgroundColor

答案 2 :(得分:2)

在我的情况下,我使用属性 android:backgroundTint="@color/some_color",它仅用于 API级别21及更高级别。例如color #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"
        >

答案 3 :(得分:1)

这应该适用于 API 17

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

答案 4 :(得分:0)

使用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>

答案 5 :(得分:0)

只需添加背景色app:cardBackgroundColor =“#0000”

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