类似于10年前提出的这个问题:
有两种颜色的带状背景?
我需要创建一个可用作通用视图背景的可绘制对象,它具有两种不同的矩形条纹颜色,就像一面旗帜。如果其中一个矩形拥有弯曲的角落,那就更好了。不幸的是,我需要动态设置颜色,所以无法在xml中定义这个可绘制对象。我该如何在Kotlin中实现这个目标?
我最好的猜测是这样的,但它不起作用:
我最好的猜测是这样的,但它不起作用:
background = LayerDrawable(
arrayOf(
GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
layout(0, 0, 100, 20)
background = styles.rootBackground
},
GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadii = floatArrayOf(8f, 8f, 8f, 8f, 0f, 0f, 0f, 0f)
layout(0, 20, 100, 40)
color = styles.robotTextBackgroundColor //requires color state list ?!
}
)
)