首页 > 解决方案 > 自定义可绘制状态

问题描述

我想以编程方式更改现有的可绘制状态列表(默认和按下)颜色。

这是我想编辑的自定义drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <ripple android:color="#1AFFFFFF">
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/colorPrimary" />
                    <corners android:radius="5dp" />
                </shape>
            </item>
        </ripple>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>

我试过如何更改 StateListDrawable 中的颜色?但我收到一个错误:

val customButtonTest: Drawable
    get() {
        val stateListDrawable = ContextCompat.getDrawable(context, R.drawable.custom_button) as StateListDrawable
        val drawableContainerState = stateListDrawable.constantState as DrawableContainer.DrawableContainerState
        val children = drawableContainerState.children
        val selectedItem = children[0] as GradientDrawable
        selectedItem.setColor(Color.parseColor("#FD00DF"))
        return stateListDrawable.current
    }


**ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.GradientDrawable**

当我打算将它用于 BandingAdapter 时,如何更改两种状态颜色并将其作为可绘制对象返回?

谢谢

** 已编辑 ---

val customButtonTest: Drawable
        get() {
            val stateListDrawable =
                ContextCompat.getDrawable(context, R.drawable.custom_button) as StateListDrawable
            val drawableContainerState =
                stateListDrawable.constantState as DrawableContainer.DrawableContainerState
            val children = drawableContainerState.children
            val pressedState = children[0] as RippleDrawable
            val defaultState = children[1] as GradientDrawable
            pressedState.setColor(ColorStateList.valueOf(Color.parseColor("#1AFFFFFF")))
            defaultState.setColor(Color.parseColor("#4267b2"))
            drawableContainerState.addChild(pressedState)
            drawableContainerState.addChild(defaultState)
            return drawableContainerState.newDrawable()
        }

如何更改波纹容器内的项目纯色?作为

pressedState.setColor(ColorStateList.valueOf(Color.parseColor("#1AFFFFFF")

改变波纹颜色本身。

标签: androidmobiledata-bindingcustomizationdrawable

解决方案


如果有人需要访问涟漪和它的孩子,这里你去:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <ripple android:color="#1A1C1C1C">
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/colorPrimary" />
                    <corners android:radius="5dp" />
                </shape>
            </item>
        </ripple>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>

  val customButtonTest: Drawable
    get() {
        val stateListDrawable = ContextCompat.getDrawable(context, R.drawable.custom_button) as StateListDrawable
        val drawableContainerState = stateListDrawable.constantState as DrawableContainer.DrawableContainerState
        val children = drawableContainerState.children
        val pressedState = children[0] as RippleDrawable
        val defaultState = children[1] as GradientDrawable
        val layerDrawable = pressedState as LayerDrawable
        val rippleChildDrawable = layerDrawable.getDrawable(0) as GradientDrawable

        rippleChildDrawable.setColor(primaryColor)
        defaultState.setColor(primaryColor)

        return drawableContainerState.newDrawable()
    }

要到达<items>内部<ripple>,您需要将 RippleDrawable 转换为 LayerDrawable


推荐阅读