【问题标题】:Android RadioGroup - will it work with additional components within?Android RadioGroup - 它可以与内部的其他组件一起使用吗?
【发布时间】:2013-10-29 22:27:30
【问题描述】:

我的想法是配对imageView -radiobutton,每对封闭在Linearlayout(水平方向)中,并且ImageButtons在选择和确认选项时会更改其背景颜色。所以,它看起来像:

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

是否可以将此层次结构存储在 RadioGroup 中?我试图应用它,但在我的应用程序中看不到 ImageViews。还是 RadioGroup 仅用于 RadioButtons 而忽略所有其他视图?

【问题讨论】:

    标签: android radio-group


    【解决方案1】:

    是的,这是可能的。如果它不起作用,则说明您的代码有其他问题。

    基于 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 的代码和动态生成的代码,在我的情况下无法正常工作,但最终我做到了。
    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多