【问题标题】:CardView Background color not the one I setCardView 背景颜色不是我设置的
【发布时间】:2019-02-28 12:08:35
【问题描述】:

我正在尝试使用 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));

关于如何解决这个问题的任何想法?谢谢!

【问题讨论】:

  • 代码在哪里??
  • 添加一些代码..
  • 使用setCardBackgroundColor() 而不是setBackgroundColor()
  • 请插入您的代码!
  • 我添加了代码,我已经准备好使用 setCardBackgroundColor()

标签: android android-recyclerview android-cardview


【解决方案1】:

改变,

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

快乐编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2016-02-28
    • 2021-06-23
    • 2017-07-07
    • 2020-07-21
    • 1970-01-01
    相关资源
    最近更新 更多