<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item>
<selector>
<item android:state_selected="true">
<layer-list>
<item android:left="-5dp"
android:top="-5dp"
android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:width="3dp"
android:color="@android:color/white"/>
<solid android:color="@android:color/transparent"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="false">
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
</shape>
</item>
</selector>
</item>
</ripple>
这是我的波纹可绘制,我想改变 state_selected , solid 颜色。
代码我试过了:
RippleDrawable rippleDrawable = (RippleDrawable) textView.getBackground(); // assumes bg is a RippleDrawable
int[][] states = new int[][]{new int[]{android.R.attr.state_selected}};
int[] colors = new int[]{R.color.white};
ColorStateList colorStateList = new ColorStateList(states, colors);
rippleDrawable.setColor(colorStateList);
不幸的是它不起作用..我错过了什么,这可能吗?
答案 0 :(得分:1)
您应该通过java / kotlin为项目添加ID 检查此后台XML文件
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#e0e0e0">
<item android:id="@+id/fab_shape">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="25dp" />
<solid android:color="@color/colorAccent" />
</shape>
</item>
</ripple>
更改此实体颜色,在constraintLayout背景上,应用此可绘制XML
val background = constraintLayout.background as RippleDrawable
val bgShape = background.findDrawableByLayerId(R.id.fab_shape) as GradientDrawable
bgShape.color = color
供参考read this