View 自定义 - 一些使用技巧

一、代码设置 Selector 图

val bg = StateListDrawable()
//按下背景图
val pressedDrawable = GradientDrawable()
pressedDrawable.setColor(resources.getColor(R.color.blue, null))
pressedDrawable.cornerRadius = 15F
bg.addState(intArrayOf(android.R.attr.state_pressed), pressedDrawable)
//正常背景图
val normalDrawable = GradientDrawable()
normalDrawable.setColor(resources.getColor(R.color.red, null))
normalDrawable.cornerRadius = 15F
bg.addState(intArrayOf(), normalDrawable)
//设置背景
val tv = TextView(this)
tv.background = bg

你可能感兴趣的:(View,android)