API 19和21上的CardView灰色

问题描述 投票:2回答:3

我不知道为什么我的卡在我的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存储加载这些图像。如您所见,我没有改变任何地方的背景颜色。

任何的想法?

android android-recyclerview cardview
3个回答
5
投票

我相信您将错误的上下文传递给适配器以使您的布局膨胀。尝试传递活动或片段上下文,而不是传递applicationConetxt。 ApplicationContext不应用您定义的主题。

要么

如果你不想这样做。你需要改变你的carview的背景颜色,如下所示:

<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>

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。


-1
投票

正如您在此链接中看到的那样

android support(cardView, RecyclerView)library in older versions with target kitkat

您的CardView仅兼容API级别26,这就是我要求您发布Gradle依赖项的原因

您可以尝试更改最低API级别,并查看是否有任何更改

© www.soinside.com 2019 - 2024. All rights reserved.