CardView背景颜色不是我设置的颜色

问题描述 投票:0回答:1

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

问题是我的元素,而不是我传入的颜色(例如:红色,蓝色或绿色),它们有不同的紫色色调。

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

这是列表的元素:

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

有关如何解决此问题的任何想法?谢谢!

android android-recyclerview android-cardview
1个回答
0
投票

更改,

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

快乐的编码。

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