【问题标题】:Custom view crash with float or fraction styleable resources使用浮动或分数样式资源的自定义视图崩溃
【发布时间】:2021-01-04 17:33:12
【问题描述】:

自定义视图:

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>

活动 XML:

<?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)
        }
    }
}

我收到以下错误:

“无法启动活动 ComponentInfo{com.example.barchart2/com.example.barchart2.MainActivity}:android.view.InflateException:二进制 XML 文件第 28 行:二进制 XML 文件第 28 行:膨胀类 com 时出错。 example.barchart2.BarView"

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

【问题讨论】:

    标签: android resources android-inflate


    【解决方案1】:

    发现我的错误: mBarWidthRatio = getFloat(R.styleable.BarView_barColor, 0f) mBarColor = getColor(R.styleable.BarView_barWidthRatio, 0) 混合了颜色和比例。

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2017-05-22
      • 2011-10-24
      相关资源
      最近更新 更多