In my app I have a header bar which consists of a single textview with fill_parent as width, which have a specific background color and some centered text. Now I want to add a d
no test too much, another way
class CenterDrawableToggleButton @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatToggleButton(context, attrs, defStyleAttr) {
private val centerMatrix = Matrix()
override fun onDraw(canvas: Canvas?) {
canvas?.save()?:return
centerMatrix.reset()
centerMatrix.postTranslate(width / 2F - (getDrawWidth() / 2), 0F)
canvas.concat(centerMatrix)
super.onDraw(canvas)
canvas.restore()
}
private fun getDrawWidth(): Float {
return paint.measureText(text.toString()) + compoundPaddingRight + compoundPaddingLeft
}
}