Transparent background on CardView - Android

与世无争的帅哥 提交于 2019-11-28 16:56:04

问题


I want to do transparent background on 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?

Regards


回答1:


Setup your CardView to use the cardBackgroundColor attribute to remove color and cardElevation attribute to remove the drop shadow. 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

If you are using an older API, you will need to call these two functions on your CardView instead:

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



回答2:


Simple 2 steps to make Android CardView transparent.

  1. Set app:cardBackgroundColor="@android:color/transparent". This is CardView attribute to set background.

  2. Set app:cardElevation="0dp" to remove the shadow.

For example, here is small xml code to create transparent 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" />

Note: Don't use setBackground. Use app:cardBackgroundColor instead.




回答3:


In my case, I used the attribute android:backgroundTint="@color/some_color",it is only used en API level 21 and higher. And color #50000000 for example.

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


This should work on API 17

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



回答5:


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


来源:https://stackoverflow.com/questions/28629549/transparent-background-on-cardview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!