简体   繁体   English

CardView 上的透明背景 - Android

[英]Transparent background on CardView - Android

I want to do transparent background on CardView.我想在 CardView 上做透明背景。 I know backgroundColor but i have image on my Layout.我知道背景颜色,但我的布局上有图像。

Do you know how do it?你知道怎么做吗? Or something which work as cardview but i will set a transparent background?或者可以用作cardview但我会设置透明背景的东西?

Regards问候

Setup your CardView to use the cardBackgroundColor attribute to remove color and cardElevation attribute to remove the drop shadow.设置您的 CardView 以使用cardBackgroundColor属性来移除颜色和cardElevation属性来移除阴影。 For example:例如:

<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"> 

For a full list of supported attributes see here: https://developer.android.com/reference/android/support/v7/widget/CardView.html有关受支持属性的完整列表,请参见此处: https : //developer.android.com/reference/android/support/v7/widget/CardView.html

If you are using an older API, you will need to call these two functions on your CardView instead:如果您使用的是较旧的 API,则需要在CardView上调用这两个函数:

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

in SDK version 21 or higher steps to make Android CardView transparent.在 SDK 版本 21 或更高版本中使 Android CardView透明的步骤。

  1. Set android:backgroundTint="@android:color/transparent" .设置android:backgroundTint="@android:color/transparent" This is CardView attribute to set background.这是用于设置背景的CardView属性。

  2. Set android:cardElevation="0dp" to remove the shadow.设置android:cardElevation="0dp"以移除阴影。

For example, here is small xml code to create transparent CardView例如,这里是创建透明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" />

In my case, I used the attribute android:backgroundTint="@color/some_color" ,it is only used en API level 21 and higher .就我而言,我使用了属性android:backgroundTint="@color/some_color" ,它仅用于API 级别 21 及更高级别 And color #50000000 for example.例如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"
        >

use app:cardBackgroundColor="@android:color/transparent"使用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>

这应该适用于API 17

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

Just add background color app:cardBackgroundColor="#0000"只需添加背景颜色 app:cardBackgroundColor="#0000"

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

You must have cardView to make image circle shape您必须有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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM