Android中带有标签的自定义电台组(Kotlin)

3z6pesqy  于 2023-04-07  发布在  Kotlin
关注(0)|答案(2)|浏览(176)

我试图创建一个自定义无线电组,我可以通过属性在顶部添加一个标签(下面的示例有一个硬编码的标签文本,我没有在这个示例中包括属性,以保持简单)。
我现在有这个电台组的代码:

class LabelRadioGroup(context: Context, attrs: AttributeSet?) : RadioGroup(context, attrs)
{
    init 
    {
        LayoutInflater.from(context).inflate(R.layout.compound_label_radiogroup, this, true)
    }
}

这个xml(compound_label_radiogroup):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <TextView
            android:id="@+id/labelTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Label" />

    <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />

</LinearLayout>

用法是这样的:

<sample.LabelRadioGroup
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      <androidx.appcompat.widget.AppCompatRadioButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Radio button 1"/>
      <androidx.appcompat.widget.AppCompatRadioButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="10dp"
              android:text="Radio button 2"/>
</sample.LabelRadioGroup>

当前的代码肯定是错误的,在LinearLayout中添加一个'RadioGroup'看起来也不对。但是我不知道如何用另一种方式来做。
现在标签显示在单选按钮前面。我希望最终结果是一个可重复使用的单选按钮组,如下所示:
标签
O单选按钮1 O单选按钮2
为了澄清,它需要可重用,所以根据使用情况,组中的单选按钮可能会有所不同。我如何才能做到这一点?

ut6juiuv

ut6juiuv1#

这只是一个可能有效的策略的猜测,我没有尝试过,可能还有一些需要解决的问题。
让你的类扩展LinearLayout而不是RadioGroup。毕竟,你的XML模板是一个LinearLayout。
在类中,重写addView(),这样如果子视图是RadioButton,则将其添加到子RadioGroup而不是顶级LinearLayout。幸运的是,RadioGroup也是LinearLayout,因此LayoutParams应该是可传递的。
这里的理论是,当包含类的XML被膨胀时,它将开始为嵌套的RadioButton调用addView,而您的代码将拦截这些视图并将其传递到RadioGroup中,以成为孙视图。

class LabelRadioGroup(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs)
{
    init {
        inflate(context, R.layout.compound_label_radiogroup, this)
    }

    private val radioGroup: RadioGroup = findViewById(R.id.radioGroup)

    override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams) {
        if (child is RadioButton) {
            radioGroup.addView(child, -1, params)
            return
        }
        super.addView(child, index, params)
    }

    // Add RadioGroup pass-through functions that you need, for example:

    fun setOnCheckedChangeListener(listener: RadioGroup.OnCheckedChangeListener) =
        radioGroup.setOnCheckedChangeListener(listener)

    
}
lymnna71

lymnna712#

您可以在LinearLayout中简单地做到这一点,如下所示,它将产生与您所需的最终结果相同的输出。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/labelTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Label" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatRadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Radio button 1" />

        <androidx.appcompat.widget.AppCompatRadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:text="Radio button 2" />
    </RadioGroup>

</LinearLayout>

相关问题