簡體   English   中英

帶有浮動或分數樣式資源的自定義視圖崩潰

[英]Custom view crash with float or fraction styleable resources

自定義視圖:

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”

它正在處理整數或字符串資源,而不是浮點數或分數。

發現我的錯誤: mBarWidthRatio = getFloat(R.styleable. BarView_barColor , 0f) mBarColor = getColor(R.styleable. BarView_barWidthRatio , 0) 混合了顏色和比例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM