java - 相同的颜色在不同的 View 中显示不同

标签 java android colors

当我在两个不同的 View 上使用相同的颜色时,我会在每个 View 上得到不同的颜色...

一个直接在XML中定义,另一个View的颜色将通过Java设置;

XML 如下:

<android.support.constraint.ConstraintLayout
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#482F8B83"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/list_title_bar">


.
.
.



</android.support.constraint.ConstraintLayout>

另一个用在 recyclerView 中,如下所示(其中 R.color.all 定义为完全等于 #482F8B83(也被使用,在上面的 XML 中)):

viewHolder.listItemBackground.setBackgroundColor(bgColor4listItem.equals("all")?R.color.all:bgColor4listItem.equals("search")?R.color.search:R.color.top);

我需要得到完全相同的颜色,但我没有!有什么解决方案?

最佳答案

您需要将颜色 int 传递给 setBackgroundColor()。当前,您正在使用各种颜色的资源 ID。您可以从给定的颜色资源 ID 中获取颜色 int,如下所示:

Context ctx = viewHolder.itemView.getContext();
int colorAll = ContextCompat.getColor(ctx, R.color.all);

关于java - 相同的颜色在不同的 View 中显示不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231800/

相关文章:

android - 调整 Android 设备的色彩空间

java - 构建 Storm 时 Zookeeper 的 ClassNotFoundException

java - 如何在 @WebMvcTest 中获取 Spring Boot 默认验证响应?

java - 检测套接字中的断开连接

android - flavor 特定代码

javascript - 我可以根据网页中的背景像素颜色更改图像像素的颜色吗?

java - 从服务器应用程序连接到 FCM 时获取 "Server returned HTTP response code: 400"

android - Gitignore 不会忽略文件夹 android,react native

android - 是否可以以具有多个图钉的 Intent 打开谷歌地图

user-interface - 给定背景颜色的良好文本前景色