CardView 背景颜色不是我设置的

CardView Background color not the one I set

我正在尝试用 CardView 个元素制作一个 RecyclerView,每个元素都有不同的颜色。我正在使用适配器并传入颜色资源。

问题是我的元素没有我传入的颜色(例如:红色、蓝色或绿色),而是具有不同深浅的紫色。

如果我对子相对布局使用相同的代码,颜色效果很好,但它们不会填满整个卡片。

这是列表的元素:

<android.support.v7.widget.CardView
    android:id="@+id/rl_main_list_background_color"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="16dp"
    android:layout_marginBottom="8dp">

    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tv_main_list_game_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Game"
            android:layout_margin="16dp"
            android:textStyle="bold|italic"
            android:textSize="20sp"/>

    </RelativeLayout>

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

这是适配器

public class GamesListAdapter extends RecyclerView.Adapter<GamesListAdapter.GamesViewHolder> {

    private ArrayList<GameItem> mGamesList;


    public static class GamesViewHolder extends RecyclerView.ViewHolder{
        public TextView mGameName;
        public CardView mItemRelativeLayour;

        public GamesViewHolder(@NonNull View itemView) {
            super(itemView);
            mGameName = itemView.findViewById(R.id.tv_main_list_game_name);
            mItemRelativeLayour = itemView.findViewById(R.id.rl_main_list_background_color);
        }
    }

    public GamesListAdapter(ArrayList<GameItem> gamesList){ mGamesList = gamesList;}


    @NonNull
    @Override
    public GamesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_list_list_element, viewGroup, false);
        GamesViewHolder gamesViewHolder = new GamesViewHolder(view);
        return gamesViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull GamesViewHolder gamesViewHolder, int i) {
            GameItem currentItem = mGamesList.get(i);

            gamesViewHolder.mGameName.setText(currentItem.getGameName());
            gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(currentItem.getBackgroundColor());
    }

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

}

这就是我填充列表的方式

gamesList = new ArrayList<>();
gamesList.add(new GameItem("Truth or Dare", R.color.truthOrDareColor));
gamesList.add(new GameItem("Kings", R.color.kingsColor));
gamesList.add(new GameItem("Flip the card(Urmatoarea Carte)", R.color.flipTheCardColor));
gamesList.add(new GameItem("Most likely to", R.color.mostLikelyToColor));
gamesList.add(new GameItem("Spin the bottle", R.color.spinTheBottleColor));
gamesList.add(new GameItem("I have sex like", R.color.iHaveSexLikeColor));
gamesList.add(new GameItem("Never have I ever", R.color.neverHaveIEverColor));

知道如何解决这个问题吗?谢谢!

改变,

gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(currentItem.getBackgroundColor());

给,

gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(
          ContextCompat.getColor(context, currentItem.getBackgroundColor()));

您正在将 Interger ID 设置为颜色 R.color.some_color 只是存储对您的颜色的引用,您必须从该 ID 获取实际颜色。

要从参考 ID 获取颜色,您应该使用以下方法,

ContextCompat.getColor(context, R.color.some_color);

快乐编码。