首页 > 解决方案 > 当我在自定义视图类中使用 R.styleable 时,我得到一个红色的 Unresolved reference: styleable

问题描述

我是初学者。这几天开始学习自定义视图,过程中几乎没有问题。

当我去谷歌解决这个问题时,有人提出了解决方案,但都没有成功。我使用 Kotlin 编写的自定义视图。

这是我的自定义视图类,名称是 MyView.kt

package com.example.demos

import android.R
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import kotlin.math.min


class MyView : View {
    //    init
    private lateinit var arcPaint: Paint
    private lateinit var progressTextPaint: Paint
//    private lateinit var arcPaintColor: Color
    private var arcPaintColor = Color.BLACK
//    private lateinit var progressTextPaintColor: Color
    private var progressTextPaintColor = Color.BLACK
    private var angle = 0f
    private var progress: Float = angle / 3.6f


    //    get/set
    fun setArcPaintColor(color: Int) {
        arcPaintColor = color
    }

    fun getArcPaintColor(): Int {
        return arcPaintColor
    }

    fun setProgressTextPaintColor(color: Int) {
        progressTextPaintColor = color
    }

    fun getProgressTextPaintColor(): Int {
        return progressTextPaintColor
    }

    fun setAngle(float: Float) {
        angle = float
        progress = angle / 3.6f
        invalidate()
    }

    fun getAngle(): Float {
        return angle
    }

    fun setProgress(float: Float) {
        progress = float
        angle = progress * 3.6f
        invalidate()
    }

    fun getProgress(): Float {
        return progress
    }

    /*call method initPaint()*/
    constructor(context: Context) : super(context) {
        initPaint()
    }

    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {
        initPaint()
    }

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        arcPaintColor = typedArray.getColor(R.styleable.arcPaintColor,)
        initPaint()
    }


    /*override onDraw(),draw view*/
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        drawView(canvas)
    }

    //init paints
    private fun initPaint() {
        arcPaint = Paint(Paint.ANTI_ALIAS_FLAG).also {
            it.color = arcPaintColor
            it.strokeWidth = 5f
            it.strokeWidth = 40f
            it.style = Paint.Style.STROKE
            it.strokeCap = Paint.Cap.ROUND
        }
        progressTextPaint = Paint(Paint.ANTI_ALIAS_FLAG).also {
            it.color = progressTextPaintColor
//            it.color = Color.GREEN
//            it.setStrokeWidth(5f)
            it.style = Paint.Style.FILL
            it.textSize = 50f
        }
    }

    /*draw view*/
    private fun drawView(canvas: Canvas?) {
        val displayWidth = width
        val displayHeight = height
        /*get center of circle*/
        val centerX = (displayWidth / 2).toFloat()
        val centerY = (displayHeight / 2).toFloat()

        /*get radius*/
        val radius = min(displayWidth, displayHeight) / 4


        val rectF = RectF(
            centerX - radius,
            centerY - radius,
            centerX + radius,
            centerY + radius
        )


        canvas?.drawArc(
            rectF,
            0f,
            angle,
            false,
            arcPaint
        )

        canvas?.drawText(
            "${String.format("%.1f", progress)}%",
            centerX - progressTextPaint.measureText("${String.format("%.1f", progress)}%") / 2,
            centerY,
            progressTextPaint
        )
    }
}

这是我的自定义属性的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="arcPaintColor" format="color"/>
        <attr name="progressTextPaintColor" format="color"/>
    </declare-styleable>
</resources>

我的自定义属性的 xml 文件

标签: androidkotlinandroid-view

解决方案


问题是您正在导入 android.R

您需要导入 [you package name].R 版本。


推荐阅读