我现在在kotlin上重新制作了旧的Java代码,但我有一个问题,我需要此代码的kotlin类似物:
Drawable[] layers = new Drawable[]{
colorDrawable,
border
};
LayerDrawable drawable = new LayerDrawable(layers);
在科特林,我现在有:
val border = GradientDrawable()
border.shape = GradientDrawable.RECTANGLE
border.setStroke(CommonsUtils.dpToPx(holder.itemView, 1), Color.parseColor("#DEDEDE"))
border.cornerRadius = CommonsUtils.dpToPx(holder.itemView, 2).toFloat()
val colorDrawable = try {
Color.parseColor(color.hex)
} catch (e: Exception) {
e.printStackTrace()
ColorDrawable(Color.parseColor("#FFFFFF"))
}
val layers = arrayOf(colorDrawable, border)
val drawable = LayerDrawable(layers as Array<out Drawable>)
但是arrayOf返回“ Any”的数组而不是“ out Drawable”的数组,并且我将类Object []转换为Drawable []。
答案 0 :(得分:2)
更改为此:
val colorDrawable = try {
ColorDrawable(Color.parseColor(color.hex))
} catch (e: Exception) {
e.printStackTrace()
ColorDrawable(Color.parseColor("#FFFFFF"))
}
Color.parseColor(color.hex)
返回int
而不是Drawable