如何更改可绘制波纹的纯色?
这里是我的波纹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
供参考阅读
- 这种类型有一个构造函数,必须在这里初始化 – Kotlin
- setSmallIcon(图标:图标)和NotificationCompat
- Android架构组件 – ViewModel Observable&Proguard
- Kotlin中AsyncTask的子类 – 无法在onPostExecute中使用主构造函数参数
- 如何以编程方式设置Android TextView中的maxLength?
- 选择后,Android微调器getDropDownView重复项目
- IOException:AsyncTask图像下载Kotlin Android(坏文件描述符)
- 使用Kotlin无法在项目中设置Realm
- SurfaceView如何重新创建,如果已经被破坏