Class 在 kotlin 中创建 LayerDrawable 时抛出异常
Class cast exception while create LayerDrawable in kotlin
我现在在 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 returns Array of "Any" 而不是 Array of "out Drawable" 我有 class 将异常 Object[] 转换为 Drawable[].
改为:
val colorDrawable = try {
ColorDrawable(Color.parseColor(color.hex))
} catch (e: Exception) {
e.printStackTrace()
ColorDrawable(Color.parseColor("#FFFFFF"))
}
Color.parseColor(color.hex)
returns int
不是 Drawable
我现在在 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 returns Array of "Any" 而不是 Array of "out Drawable" 我有 class 将异常 Object[] 转换为 Drawable[].
改为:
val colorDrawable = try {
ColorDrawable(Color.parseColor(color.hex))
} catch (e: Exception) {
e.printStackTrace()
ColorDrawable(Color.parseColor("#FFFFFF"))
}
Color.parseColor(color.hex)
returns int
不是 Drawable