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

q8l4jmvw  于 2023-03-16  发布在  Android
关注(0)|答案(7)|浏览(775)

我有一张卡片,想给它添加内容。我应该如何添加图片和文本到卡片?下面是我的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>

下面是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);

    }
}

card1.setCardBackgroundColor(100);也没有改变背景色。那么有没有人能帮我添加一张图片呢?

2g32fytz

2g32fytz1#

无法将图像设置为卡片视图的背景图像。但您可以使用setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))使用背景颜色
如果您想在CardView内部设置背景图像,请使用其他布局,如LinearLayoutRelativeLayout或CardView内部的任何其他布局。并为该布局添加背景。这是为CardView设置背景图像的简单方法之一

8wigbo56

8wigbo562#

你可以在不损失卡角半径的情况下完成这个操作。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@drawable/zoneback"
    android:layout_height="match_parent"
    tools:context=".kidzone">

    <android.support.v7.widget.CardView
        android:layout_marginTop="75dp"
        android:id="@+id/quizcard"
        android:elevation="15dp"
        app:cardPreventCornerOverlap="false"
        android:layout_width="match_parent"
        app:cardCornerRadius="50dp"
        android:layout_marginHorizontal="50dp"
        android:layout_height="250dp">
        <ImageView
            android:layout_width="match_parent"
            android:id="@+id/quizimage"
            android:layout_height="match_parent" />

    </android.support.v7.widget.CardView>
</LinearLayout>

您必须创建一个自定义Drawable

public class RoundCornerDrawable extends Drawable {
    private final float mCornerRadius;
    private final RectF mRect = new RectF();
    //private final RectF mRectBottomR = new RectF();
    //private final RectF mRectBottomL = new RectF();
    private final BitmapShader mBitmapShader;
    private final Paint mPaint;
    private final int mMargin;

    public RoundCornerDrawable(Bitmap bitmap, float cornerRadius, int margin) {
        mCornerRadius = cornerRadius;

        mBitmapShader = new BitmapShader(bitmap,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(mBitmapShader);

        mMargin = margin;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin);
        //mRectBottomR.set( (bounds.width() -mMargin) / 2, (bounds.height() -mMargin)/ 2,bounds.width() - mMargin, bounds.height() - mMargin);
        // mRectBottomL.set( 0,  (bounds.height() -mMargin) / 2, (bounds.width() -mMargin)/ 2, bounds.height() - mMargin);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
        //canvas.drawRect(mRectBottomR, mPaint); //only bottom-right corner not rounded
        //canvas.drawRect(mRectBottomL, mPaint); //only bottom-left corner not rounded

    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

}

最后,这是我的活动代码:

RoundCornerDrawable round = new RoundCornerDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.quizcardback),
            getResources().getDimension(R.dimen.cardview_radius), 0);
    ImageView imageView=root.findViewById(R.id.quizimage);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        imageView.setBackground(round);
    else
        imageView.setBackgroundDrawable(round);
r1zhe5dt

r1zhe5dt3#

我正在使用的卡背景绘制如下。

<androidx.cardview.widget.CardView
            android:layout_width="100dp"
            android:layout_height="75dp"
            android:layout_margin="8dp"
            android:elevation="8dp"
            app:cardCornerRadius="8dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/event"></LinearLayout>
        </androidx.cardview.widget.CardView>
myss37ts

myss37ts4#

1.为了设置卡片的背景图像,我们必须添加relative或LinearLayout
1.在Cardview声明后添加RelativeLayout,这样你就可以在卡片中移动元素。3.添加下面的代码/示例如下

android:layout_width="match_parent"
android:layout_height="489dp"
android:layout_margin="10dp"
android:orientation="vertical"
app:cardBackgroundColor="@color/cardview"
app:cardCornerRadius="7dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="false">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/background">

4.其中android:background="@drawable/background">是我的映像名称。

yqhsw0fo

yqhsw0fo5#

你试过这个吗?

card1.setBackgroundResource(R.drawable.yourimage);
4smxwvx5

4smxwvx56#

我知道它的答案太晚了,但我有一个简单和聪明的方法,你做一个卡视图,然后在它的另一个视图组,然后添加backgroundTint为您的卡视图,并把不透明度为0,然后设置背景图像为您的其他布局,你只是在您的卡视图中定义。

lvmkulzt

lvmkulzt7#

尝试在线性布局中设置背景,如下所示:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/googleshape1"
        android:orientation="horizontal">

相关问题