我想在CardView上实现透明背景。 我知道backgroundColor,但是我的布局上有图片。
你知道如何做吗?或者有什么可以像cardview一样工作,但我可以设置透明背景的东西吗?
问候
我想在CardView上实现透明背景。 我知道backgroundColor,但是我的布局上有图片。
你知道如何做吗?或者有什么可以像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);
setCardElevation()
和 setCardBackgroundColor()
方法来设置你的 CardView
。请查看我的修改。 - Chris Stillwell@null
但没有成功,有什么想法吗? - Gokhan Arik在SDK版本21或更高版本中,使Android CardView
透明的步骤。
设置 android:backgroundTint="@android:color/transparent"
。这是用于设置背景的CardView
属性。
设置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" />
我使用的是 属性 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"
>
使用 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));
只需添加背景颜色 app:cardBackgroundColor="#0000"
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#0000">
你必须拥有 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>
<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>
<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>
android:background="@android:color/transparent"
吗? - Psypher