gpt4 book ai didi

android - 当我在自定义 View 类中使用 R.styleable 时,我得到一个红色的 Unresolved reference : styleable

转载 作者:行者123 更新时间:2023-12-02 13:41:44 26 4
gpt4 key购买 nike

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

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

这是我的自定义 View 类,名字是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>

the xml file of my custom attribute

最佳答案

问题是您正在导入 android.R

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

关于android - 当我在自定义 View 类中使用 R.styleable 时,我得到一个红色的 Unresolved reference : styleable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62673222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com