如何在CardView中设置卡的背景图像?

Ved*_*ant 4 android android-cardview

我有一张卡,想要添加内容.我应该如何在卡片中添加图片和文字?这是我的xml代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ml.vedantk.app.god.MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/card1"
        android:layout_width="364dp"
        android:layout_height="389dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="64dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这是java文件:

package ml.vedantk.app.god;

import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CardView card1 = (CardView)findViewById(R.id.card1);
        card1.setCardBackgroundColor(100);

    }
}
Run Code Online (Sandbox Code Playgroud)

card1.setCardBackgroundColor(100);还没有改变背景颜色.那么有人可以帮我添加图像吗?

Tom*_*thu 11

图像无法设置为卡片视图的背景图像.但您可以使用背景颜色 setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))

如果你想设置背景图片里面Cardview使用另一种布局,例如LinearLayout,RelativeLayout或任何其他内部的CardView.并为该布局添加背景.这是为CardView设置BackgroundImage的简单方法之一