gpt4 book ai didi

android - Cardview 设置背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:30 26 4
gpt4 key购买 nike

我正在尝试像这样在 RecylerView 的 Bindview 持有者中动态更改 Cardview 背景颜色。

holder.cardView.setCardBackgroundColor(R.color.LightCyan);

奇怪的是背景应用的颜色几乎与所应用的颜色相反。(#E0FFFF-LightCyan)到 1F0000 -Almost Black)

我在这里验证了几种颜色 here结果是一样的。

但是如果我这样设置

holder.cardView.setCardBackgroundColor(ContextCompat.getColor(this.mContext, R.color.LightCyan));

效果很好。(是的,这是正确的设置方式)。

卡片 View XML:

<android.support.v7.widget.CardView
android:id="@+id/cv"
android:foreground="?selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">

操作系统:Android 6.0 HTC

但是这里的理解差距在哪里?

最佳答案

方法setCardBackgroundColor采用 color 参数,这意味着颜色以 ARGB 格式表示为 4 字节整数,但您传递给方法 R.color.LightCyan 这不是颜色,而是应用程序/系统资源中的颜色索引。要获得颜色,您应该使用 Color.argb(int alpha, int red, int green, int blue)Resources.getColor(int index, Theme theme)或使用 ContextCompat 在旧平台上使用它。

关于android - Cardview 设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36401330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com