如何在安卓中将可绘制形状渐变应用于android.support.v7.widget.CardView



我无法将背景渐变设置为android.support.v7.widget.CardView

使用app:cardBackgroundColor="@somecolor"我只设置背景颜色,而不是可绘制的资源文件

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@drawable/bg"> // not working in CardView
</android.support.v7.widget.CardView>

BG.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:angle="45" android:startColor="@android:color/black"
        android:endColor="@android:color/black"
        android:centerColor="@android:color/holo_red_dark"
        android:centerX="4"
        />
</shape>

这是用于实现目标的代码

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/bg">
        <!--Your View-->
  </LinearLayout>
</android.support.v7.widget.CardView>
代码

中的

cardView.setBackgroundResource(R.drawable.shape_sina_pay_bank_bg);

最新更新