自定义TextView文本不可见

时间:2018-06-05 14:57:01

标签: android kotlin textview

我有一个简单的自定义TextView,可以添加带圆角的背景颜色。但是,正如您从绿色小方块中看到的那样,文本不可见。我尝试在XML文件中设置text和textColor,同样的事情。我哪里做错了?

enter image description here

class StatusRectView(context: Context, attrs: AttributeSet) : TextView(context, attrs) {

    private var cornerRadius = 0.8f
    private val paint = Paint()
    private var size = 0  

    init {
        paint.isAntiAlias = true
        gravity = Gravity.CENTER
        setTextColor(Color.WHITE)
        text = "D"
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        size = Math.min(measuredWidth, measuredHeight)
        setMeasuredDimension(size, size)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        paint.color = Color.GREEN
        paint.style = Paint.Style.FILL

        val rectF = RectF(0f,  0f, size * 1f, size * 1f)
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint)
    }
}

3 个答案:

答案 0 :(得分:1)

您正在基类内容上绘制矩形。

答案 1 :(得分:1)

绘制矩形后,您必须调用gem uninstall chromedriver-helper

答案 2 :(得分:0)

您必须在自定义 textView 的 onDraw 方法中调用 super.onDraw。