我有一张卡片,想要添加内容。我应该如何在卡片中添加图片和文字?这是我的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);
也没有改变背景颜色。那么有人可以帮我添加图像吗?
答案 0 :(得分:7)
图像无法设置为卡片视图的背景图像。但您可以使用setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))
如果要在Cardview中设置背景图像使用另一个布局,例如LinearLayout
,RelativeLayout
或任何其他内部CardView。并为该布局添加背景。这是为CardView设置BackgroundImage的简单方法之一
答案 1 :(得分:1)
我知道答案很晚了,但是我为您提供了简单而智能的方法 制作卡片视图,然后在其中创建另一个视图组 然后为您的卡片视图添加backgroundTint并将不透明度设置为0 然后为您刚刚在卡片视图中定义的其他布局设置背景图片。
答案 2 :(得分:1)
我使用卡片背景可绘制如下。
<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>
答案 3 :(得分:0)
你试过这个吗?
card1.setBackgroundResource(R.drawable.yourimage);
答案 4 :(得分:0)
LinearLayout
。在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">
是我的图片名称。
答案 5 :(得分:0)
您可以执行此操作而不会丢失您的卡角半径。 这是我的XML:
<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);