Android RadioGroup-它是否可以与其他组件一起工作?

Android RadioGroup-它是否可以与其他组件一起工作?,android,radio-group,Android,Radio Group,我的想法是让成对的ImageView-RadioButton,每对都包含在LinearLayout(水平方向)中,当选择并确认一个选项时,ImageButton的背景色会发生变化。所以,它看起来像: [LinearLayout1]: [ImageView1] [RadioButton1] [LinearLayout2]: [ImageView2] [RadioButton2] [LinearLayout3]: [ImageView3] [RadioButton3] 是否可以在RadioGrou

我的想法是让成对的ImageView-RadioButton,每对都包含在LinearLayout(水平方向)中,当选择并确认一个选项时,ImageButton的背景色会发生变化。所以,它看起来像:

[LinearLayout1]: [ImageView1] [RadioButton1]
[LinearLayout2]: [ImageView2] [RadioButton2]
[LinearLayout3]: [ImageView3] [RadioButton3]

是否可以在RadioGroup中存储此层次结构?我尝试应用此功能,但在我的应用程序中没有看到ImageView。还是仅为单选按钮创建RadioGroup,而忽略所有其他视图?

是的,这是可能的。如果它不起作用,那么您的代码还有其他问题

基于SDK中包含的ApiDemos示例Android应用程序的概念验证布局:

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:checkedButton="@+id/lunch"
    android:id="@+id/menu">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_breakfast"
        android:id="@+id/breakfast"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/btn_star"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_group_1_lunch"
            android:id="@id/lunch"/>
    </LinearLayout>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_dinner"
        android:id="@+id/dinner"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_all"
        android:id="@+id/all"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_selection"
        android:id="@+id/choice"/>
</RadioGroup>

以及它产生的布局:


是的,这是可能的。如果它不起作用,那么您的代码还有其他问题

基于SDK中包含的ApiDemos示例Android应用程序的概念验证布局:

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:checkedButton="@+id/lunch"
    android:id="@+id/menu">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_breakfast"
        android:id="@+id/breakfast"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/btn_star"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_group_1_lunch"
            android:id="@id/lunch"/>
    </LinearLayout>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_dinner"
        android:id="@+id/dinner"/>


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_all"
        android:id="@+id/all"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_selection"
        android:id="@+id/choice"/>
</RadioGroup>

以及它产生的布局:


你说得对,这是可以做到的,谢谢!因为基于XML的代码和动态生成的代码混合在一起,所以在我的案例中不起作用,但最终我做到了。你说得对,这是可以做到的,谢谢!由于基于XML的代码和动态生成的代码混合在一起,所以在我的案例中不起作用,但最终我做到了。