You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改作为RecyclerView Item卡片背景的Drawable XML中元素的颜色?

How to Change Element Colors in Your RecyclerView Item Background Drawable XML

Hey there! Let's cover both static (XML-based) and dynamic (code-based) ways to update the colors in your layer-list drawable, since you're using it as a RecyclerView item background:

1. Static Color Changes (Directly in XML)

Your current drawable uses a layer-list with a primary color background. To update this statically:

Update the main background color

You can either replace the existing color reference with a new one, or use a raw hex color value:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Option 1: Use a color resource -->
    <item android:drawable="@color/your_new_primary_color" android:id="@+id/background_color"/>
    
    <!-- OR Option 2: Use a hex color directly -->
    <!-- <item android:drawable="#FF2196F3" android:id="@+id/background_color"/> -->

    <item android:top="200dp" android:bottom="-300dp" android:left="0dp" android:right="-300dp">
        <rotate android:fromDegrees="-10" android:pivotX="0%" android:pivotY="100%">
            <!-- Assuming your shape is a rectangle; add a solid color here -->
            <shape android:shape="rectangle">
                <solid android:color="@color/your_rotate_shape_color"/>
                <!-- Optional: Add corners, stroke, etc. -->
            </shape>
        </rotate>
    </item>
</layer-list>

2. Dynamic Color Changes (In Kotlin/Java Code)

Since you've assigned an android:id to the main background item, you can modify its color at runtime (perfect for RecyclerView items that need dynamic theming, like selection states).

In your RecyclerView Adapter's onBindViewHolder method:

Kotlin Example:

// Get the layer drawable from the item view
val layerDrawable = holder.itemView.background as LayerDrawable
// Find the specific layer by ID
val backgroundLayer = layerDrawable.findDrawableByLayerId(R.id.background_color) as ColorDrawable
// Update the color
backgroundLayer.color = ContextCompat.getColor(holder.itemView.context, R.color.dynamic_color)

// If you also need to update the rotated shape's color:
// 1. First add an ID to the <item> containing the rotate element in XML:
// <item android:id="@+id/rotated_shape_item" android:top="200dp" ...>
// 2. Then in code:
val rotatedShapeLayer = layerDrawable.findDrawableByLayerId(R.id.rotated_shape_item) as RotateDrawable
val shapeDrawable = rotatedShapeLayer.drawable as GradientDrawable
shapeDrawable.setColor(ContextCompat.getColor(holder.itemView.context, R.color.dynamic_shape_color))

Java Example:

// Get the layer drawable from the item view
LayerDrawable layerDrawable = (LayerDrawable) holder.itemView.getBackground();
// Find the specific layer by ID
ColorDrawable backgroundLayer = (ColorDrawable) layerDrawable.findDrawableByLayerId(R.id.background_color);
// Update the color
backgroundLayer.setColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.dynamic_color));

// To update the rotated shape's color (after adding an ID to its parent <item>):
RotateDrawable rotatedShapeLayer = (RotateDrawable) layerDrawable.findDrawableByLayerId(R.id.rotated_shape_item);
GradientDrawable shapeDrawable = (GradientDrawable) rotatedShapeLayer.getDrawable();
shapeDrawable.setColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.dynamic_shape_color));

Key Notes:

  • Make sure you cast the drawables correctly (e.g., ColorDrawable for solid color items, GradientDrawable for shape elements).
  • For dynamic changes, always use ContextCompat.getColor() to handle compatibility across Android versions.

内容的提问来源于stack exchange,提问作者Varun Joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:30:56