Android 使用浮动或分数样式化资源的自定义视图崩溃

Android 使用浮动或分数样式化资源的自定义视图崩溃,android,resources,android-inflate,Android,Resources,Android Inflate,自定义视图: class BarView : View { private var mBarPaint = Paint(ANTI_ALIAS_FLAG) private var mBarWidthRatio = 0f private var mBarColor = Color.BLACK var barWidthRatio get() = mBarWidthRatio set(value) { mBarW

自定义视图:

class BarView : View {

    private var mBarPaint = Paint(ANTI_ALIAS_FLAG)
    private var mBarWidthRatio = 0f
    private var mBarColor = Color.BLACK

    var barWidthRatio
        get() = mBarWidthRatio
        set(value) {
            mBarWidthRatio = value
            invalidate()
        }

    var barColor
        get() = mBarColor
        set(value) {
            mBarColor = value
            invalidate()
        }

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        context.obtainStyledAttributes(attrs, R.styleable.BarView).apply {
            mBarWidthRatio = getFloat(R.styleable.BarView_barColor, 0f)
            mBarColor = getColor(R.styleable.BarView_barWidthRatio, 0)
            recycle()
        }
    }

    override fun onDraw(canvas: Canvas) {
        mBarPaint.color = mBarColor
        canvas.drawRect(0f, 0f, width * mBarWidthRatio, height.toFloat(), mBarPaint)
    }

}
可设置样式的资源:

<resources>
    <declare-styleable name="BarView">
        <attr name="barColor" format="color" />
        <attr name="barWidthRatio" format="float" />
    </declare-styleable>
</resources>
我得到以下错误:

“无法启动活动组件信息{com.example.barchart2/com.example.barchart2.MainActivity}:android.view.InflateException:二进制XML文件行#28:二进制XML文件行#28:膨胀类com.example.barchart2.BarView时出错”

它使用的是整数或字符串资源,而不是浮点或分数。

发现了我的错误: mBarWidthRatio=getFloat(R.styleable.BarView\u barColor,0f) mBarColor=getColor(R.styleable.条视图\u条宽比,0) 把颜色和比例弄混了

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="134dp" />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.example.barchart2.BarView
        android:id="@+id/bar"
        android:layout_width="200dp"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:barWidthRatio="0.75" />


</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bar.setBackgroundColor(-10000)

        button.setOnClickListener() {
            println("Width: " + bar.width)
            println("Height: " + bar.height)
            println("Left: " + bar.left)
            println("Top: " + bar.top)
            println("Right: " + bar.right)
            println("Bottom: " + bar.bottom)
        }
    }
}