当我在两个不同的 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/