API 19和21上CardView显示为灰色

5
我不知道为什么我的API 21和19模拟器上的卡片会显示为灰色。我之前用CardViews创建了RecyclerViews,那时它们是白色的。我没有在任何地方更改背景颜色。在我的API 26模拟器上它看起来正常。
以下是我的卡片布局:

enter image description here

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Name"
        android:textColor="@android:color/black"
        android:textSize="20sp" />


    <ImageView
        android:id="@+id/image_view_upload"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/text_view_name" />

</RelativeLayout>

这是我的适配器类:

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

private Context mContext;
private List<Upload> mUploads;


public ImageAdapter(Context context, List<Upload> uploads) {
    mContext = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Upload upload = mUploads.get(position);
    holder.textViewName.setText(upload.getName());
    Picasso.with(mContext)
            .load(upload.getImageUrl())
            .fit()
            .centerCrop()
            .into(holder.imageView);

}

@Override
public int getItemCount() {
    return mUploads.size();
}

class ImageViewHolder extends RecyclerView.ViewHolder {
    TextView textViewName;
    ImageView imageView;

    ImageViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);
    }
}

我从Firebase存储加载这些图片。您可以看到,我没有在任何地方更改背景颜色。

有什么想法吗?


我确实看过了,但他改变了背景颜色 - 我不想这样做。 - Florian Walther
如果你在谈论卡片视图中文本视图的背景,那么我相信你需要查看你的styles.xml文件。 - Umair
嘿,你是对的。这是因为我将getApplicationContext传递给了适配器。我在教程中找到了这个方法。如果我传递Activity上下文,它就会变成白色。现在我只需要弄清楚是更改上下文还是卡片背景。 - Florian Walther
太好了,我现在应该把它作为一个答案添加进去。 - Umair
可以理解。请继续,我会接受它。 - Florian Walther
显示剩余8条评论
3个回答

8

我相信你在为你的适配器填充布局时传递了错误的上下文。尝试传递活动片段上下文,而不是传递应用程序上下文ApplicationContext不适用于您定义的主题。

或者

如果你不想这样做,你需要改变你的卡片视图背景颜色,像这样:

<android.support.v7.widget.CardView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:card="http://schemas.android.com/apk/res-auto"
 card:cardBackgroundColor="@color/colorPrimary"
 android:layout_margin="8dp">
</android.support.v7.widget.CardView>

谢谢,这是因为我将“getApplicationContext”传递给了适配器。 - Florian Walther
很高兴能够帮到你。祝你编程愉快 :) - Umair

1

我不确定为什么在API 21上显示灰色。

但是如果你想要白色背景,那么可以像这样设置背景颜色。

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#FFFFFF">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Name"
        android:textColor="@android:color/black"
        android:textSize="20sp" />


    <ImageView
        android:id="@+id/image_view_upload"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/text_view_name" />

</RelativeLayout>

同时请注意,仅支持API 21及以上的cardview。


我更想了解为什么它显示为灰色,而我的其他使用相同卡片的项目却没有这种情况。 - Florian Walther

0

这是一条注释,而不是答案。 - Umair
实现 'com.android.support:cardview-v7:26.1.0' 实现 'com.android.support:recyclerview-v7:26.1.0' - Florian Walther
我已经更新了答案,因为在我达到50个声望之前我无法发表评论。 - theboringdeveloper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接