CardView上的透明背景 - Android

mac*_*229 69 android background android-cardview

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

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

问候

Chr*_*ell 139

设置您的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"> 
Run Code Online (Sandbox Code Playgroud)

有关支持的属性的完整列表,请参阅此处:https://developer.android.com/reference/android/support/v7/widget/CardView.html

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

myCardView.setCardBackgroundColor(Color.TRANSPARENT);
myCardView.setCardElevation(0);
Run Code Online (Sandbox Code Playgroud)

  • @MuneebMirza从你的'CardView`代码中调用`setCardElevation()`和`setCardBackgroundColor()`看到我的编辑. (2认同)

Rah*_*ina 7

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

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

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

例如,下面是创建透明的小型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" />
Run Code Online (Sandbox Code Playgroud)

注意:请勿使用setBackground。使用app:cardBackgroundColor代替。


小智 6

就我而言,我使用了属性 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"
        >
Run Code Online (Sandbox Code Playgroud)