如何更改可绘制波纹的纯色?

                      

这里是我的波纹drawable,我想改变state_selected纯色

我试过的代码:

 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); 

不幸的是它不工作..我错过了什么,这可能吗?

你应该添加一个id到项目通过java / kotlin访问它们。
检查这个背景XML文件

          

改变这个的纯色,在constraintLayout背景上,这个可绘制的XML被应用

 val background = constraintLayout.background as RippleDrawable val bgShape = background.findDrawableByLayerId(R.id.fab_shape) as GradientDrawable bgShape.color = color 

供参考阅读