1. 用xml画普通圆
- 效果图
- 用xml的shape实现
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF68F" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
- 用代码实现
/**
* 获取圆型背景
* @param colorInt 整型色值
*/
fun getCircleBg(colorInt: Int) = GradientDrawable().also {
it.shape = GradientDrawable.OVAL
it.setColor(colorInt)
}
/**
* 获取圆型背景
* @param colorStr 色值字符串,如:#ffdd00
*/
fun getCircleBg(colorStr: String) = getCircleBg(Color.parseColor(colorStr))
2.用xml画带边框的圆
- 效果图
- 用xml的shape实现
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF68F" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
- 用代码实现
如果要画空心圆,就不要调用“setColor”方法
private fun getCircle() = GradientDrawable().also {
it.shape = GradientDrawable.OVAL
val strokeWidth = 4 // 边框宽度
val strokeColor = Color.parseColor("#FFFFFF") //边框颜色
it.setStroke(strokeWidth, strokeColor)
it.setColor(Color.parseColor("#FFF68F"))
}