如何创建RadioButton自定义视图?

问题描述 投票:0回答:1

我在互联网上尝试了很多教程和解决方案,但无法使我的工作成功。 基本上我想要这样的布局作为 7 个单选按钮:

我有我的 CustomRadioButton 类,我希望它的行为像 RadioButton 一样,在普通 RadioGroup 中使用。这就是为什么我要延长

AppCompatRadioButton

问题是我无法膨胀我的 LayoutWeekDayBinding,因为父级不是 ViewGroup。它给了我这个错误消息:

Type mismatch. Required: ViewGroup? Found: CustomRadioButton

class CustomRadioButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatRadioButton(context, attrs, defStyleAttr) {

    private lateinit var binding: LayoutWeekDayBinding

    var weekDay: String = ""
        get() = field
        set(value) {
            binding.textWeekDay.text = value
            field = value
        }

    init {
        initView(context)
    }

    private fun initView(context: Context) {
        binding = LayoutWeekDayBinding.inflate(LayoutInflater.from(context), this, true)
        // error in this keyword above
    }
}

我的layout_week_day.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/layout_week_day_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true">

    <TextView
        android:id="@+id/textWeekDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:fontFamily="@font/montserrat_medium"
        android:textColor="@color/brownish_grey"
        android:textSize="14sp"
        android:textStyle="normal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textWeekTemperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/montserrat_regular"
        android:textColor="@color/brownish_grey"
        android:textSize="14sp"
        android:textStyle="normal"
        app:layout_constraintEnd_toEndOf="@+id/textWeekDay"
        app:layout_constraintStart_toStartOf="@+id/textWeekDay"
        app:layout_constraintTop_toBottomOf="@+id/textWeekDay" />
</androidx.constraintlayout.widget.ConstraintLayout>

我确实尝试过其他“膨胀”方法,但没有一个起作用:

private fun initView(context: Context) {
    binding = LayoutWeekDayBinding.inflate(LayoutInflater.from(context))

    val view = LayoutInflater.from(context).inflate(R.layout.layout_week_day, null)
    binding = LayoutWeekDayBinding.bind(view)
}

我在这里缺少什么?

android kotlin android-layout android-custom-view android-viewbinding
1个回答
0
投票

建造者是正确的。您致电:

binding = LayoutWeekDayBinding.inflate(LayoutInflater.from(context), this, true)

“this”是从 AppCompatRadioButton (CustomRadioButton) 扩展的类,但 AppCompatRadioButton 根本不扩展 ViewGroup。顺序是:

java.lang.Object
   ↳    android.view.View
       ↳    android.widget.TextView
           ↳    android.widget.Button
               ↳    android.widget.CompoundButton
                   ↳    android.widget.RadioButton
                       ↳    androidx.appcompat.widget.AppCompatRadioButton

https://developer.android.com/reference/androidx/appcompat/widget/AppCompatRadioButton

因此,您无法使用 AppCompatRadioButton 并更改其内容。这是一个按钮。

您可以做的是实现一个工作原理相同的类似视图,并且由于 RadioButton 非常简单,因此根本不会花很长时间。从 FrameLayout 或其他一些 ViewGroup 扩展,并记住它的状态。您还需要关闭您自己的 RadioGroup 替代方案中的其他功能

© www.soinside.com 2019 - 2024. All rights reserved.