#android #resources #android-inflate
#Android #Ресурсы #android-раздувать
Вопрос:
Пользовательский вид:
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>
Activity 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)
}
}
}
Я получаю следующую ошибку:
«Не удается запустить activity ComponentInfo{com.example.barchart2/com.example.barchart2.MainActivity}: android.view.Исключение InflateException: строка двоичного файла XML # 28: Строка двоичного файла XML # 28: ошибка при раздувании класса com.example.barchart2.BarView «
Он работал с целочисленными или строковыми ресурсами, но не с плавающей точкой или дробью.
Ответ №1:
Обнаружена моя ошибка: mBarWidthRatio = getFloat(R.styleable.BarView_barColor, 0f) mBarColor = getColor(R.styleable.BarView_barWidthRatio, 0) Перепутал цвет и соотношение.