XML Shape/Selector → Kotlin 动态创建
老写法(XML drawable)
res/drawable/bg_rounded_button.xml:
xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#4CAF50"/>
<corners android:radius="8dp"/>
<stroke android:width="1dp" android:color="#388E3C"/>
</shape>
res/drawable/selector_button.xml:
xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#388E3C"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#4CAF50"/>
<corners android:radius="8dp"/>
</shape>
</item>
</selector>
Java 中使用:
java
view.setBackgroundResource(R.drawable.selector_button);
问题在哪里
XML drawable 有三个局限:颜色写死不能动态改、不同状态下要写多份 shape 重复代码、需要在 res/drawable 里维护一堆小文件。
新写法(Kotlin 动态创建)
kotlin
fun roundedButtonBg(
context: Context,
normalColor: Int,
pressedColor: Int,
cornerRadiusDp: Int,
strokeColor: Int,
strokeWidthDp: Int = 1
): StateListDrawable {
val density = context.resources.displayMetrics.density
val radius = cornerRadiusDp * density
val strokeWidth = strokeWidthDp * density
val normal = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
setColor(normalColor)
cornerRadius = radius
setStroke(strokeWidth.toInt(), strokeColor)
}
val pressed = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
setColor(pressedColor)
cornerRadius = radius
setStroke(strokeWidth.toInt(), strokeColor)
}
return StateListDrawable().apply {
addState(intArrayOf(android.R.attr.state_pressed), pressed)
addState(intArrayOf(), normal)
}
}
使用:
kotlin
view.background = roundedButtonBg(
context = context,
normalColor = Color.parseColor("#4CAF50"),
pressedColor = Color.parseColor("#388E3C"),
cornerRadiusDp = 8,
strokeColor = Color.parseColor("#388E3C")
)
一句话注意
GradientDrawable.cornerRadius 和 setStroke 的参数单位是像素,不是 dp。传 dp 值必须乘以 density,否则不同分辨率手机上圆角大小不一致。StateListDrawable.addState 的状态数组顺序很重要,越具体的状态(如 state_pressed)要放在前面,默认状态(空数组)放最后。
Java Android 老项目迁移系列,持续更新中。