android - Cardview 设置背景颜色

标签 android xml android-layout android-studio android-cardview

我正在尝试像这样在 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/

相关文章:

c# - 将数据更新到 XML 文件 c#

android - 如何在android中创建像Iphone Edittext框一样的渐变

Android audiorecord 实例失败

android - Phonegap CLI 不创建 .apk 文件

xml - 如何在 XML 中显示 Activity 中的整数?

带有文本的Android自定义后退按钮

android - 表格行内的 TextView 内容被截断

android - 如何唯一标识一个安卓设备?

java - 从服务更新 UI 的更有效方法?

ruby - 如果我的 XML 文档指令指定 UTF-8 编码,我还需要转义字符吗?