android - 自定义可绘制状态
问题描述
我想以编程方式更改现有的可绘制状态列表(默认和按下)颜色。
这是我想编辑的自定义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")
改变波纹颜色本身。
解决方案
如果有人需要访问涟漪和它的孩子,这里你去:
<?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
推荐阅读
- java - 如何通过 HtmlUnitDriver 和 HtmlUnit 无头浏览器在 Pick a Category (US) 下创建以不同方式呈现的项目列表?
- kotlin - TreeSet中的HeadSet、TailSet、SubSet
- react-native - 反应原生
- asp.net - 使用自定义验证器控件条件是入口应该是数字和字母
- java - clientHello 中的 TLS 1.2 客户端密码列表对于不同的 https 服务器主机的顺序不同
- github-api - 无法使用 Github API 向拉取请求发布评论
- typescript - RaphaelJS 不会使用 typescript 构建
- ios - iOS 推送通知和 Testflight 使用 ..p8 证书和 apn
- azure - Azure 逻辑应用程序 - 通过 powershell 更新 Blob API 连接
- java - 如何在责任链中跳过空值?