我试图在RecylerView的Bindview持有者中动态更改Cardview背景颜色.
holder.cardView.setCardBackgroundColor(R.color.LightCyan);
奇怪的是背景正在应用与应用颜色几乎相反的方式.(#E0FFFF-LightCyan)到1F0000-几乎黑色)
我在这里验证了很少的颜色here,结果是一样的.
但如果我这样设置
holder.cardView.setCardBackgroundColor(ContextCompat.getColor(this.mContext, R.color.LightCyan));
它工作得很好.(是的,这是正确的设置方式).
CardView XML:
android:id="@+id/cv"
android:foreground="?selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
操作系统:Android 6.0 HTC
但这里的理解差距在哪里?
解决方法:
方法setCardBackgroundColor采用颜色参数,这意味着颜色表示为ARGB格式的4字节整数,但是您传入的方法是R.color.LightCyan,它不是颜色,而是应用程序/系统资源内部的颜色索引.要获得颜色,您应该使用Color.argb(int alpha, int red, int green, int blue)或Resources.getColor(int index, Theme theme)或使用ContextCompat在旧平台上使用它.
标签:android,android-layout,xml,android-studio,android-cardview
来源: https://codeday.me/bug/20190527/1167195.html