【问题标题】:Have horizontal RadioButtons wrap if too long for screen如果屏幕太长,让水平 RadioButtons 换行
【发布时间】:2012-08-09 20:03:00
【问题描述】:

所以我有以下单选按钮。我想让它们像这样显示:

但是会发生这种情况:

我怎样才能让它像上面那样显示?
我可以在 Eclipse 的 GUI 编辑器中移动它,但它会从 RadioGroup 中删除 RadioButton!
在组内,它会忽略所有其他布局参数。

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/timeBar"
    android:layout_marginTop="43dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/privRadio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Everyone" />

    <RadioButton
    android:id="@+id/privRadio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="FriendOfFriends" />


 <RadioButton
    android:id="@+id/privRadio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Friends" />
</RadioGroup>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    你可以简单地复制这个类:

    https://github.com/jevonbeck/AbstractMachine/blob/jevon_dev/app/src/main/java/org/ricts/abstractmachine/ui/utils/MultiLineRadioGroup.java

    到您项目中的适当包中并像这样在 XML 中实例化:

    <view
        class="mypackage.packagepath.MultiLineRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"/>
    

    【讨论】:

    • 看起来很棒的代码,但并不能真正适应 RadioGroup 中 RadioButtons 的边距。
    • 这工作正常,除非在单选按钮上设置可绘制选择器,然后它不会运行。有什么解决办法吗?
    • 这在以编程方式使用时会出现一些问题。例如,OnCheckedChangeListener 在运行时更改单选按钮时给出不正确的索引。另一个问题是,margin 在通过代码膨胀按钮时会丢失。
    【解决方案2】:

    您要的是FlowLayout。与 0gravity 更“静态”的解决方案相比,这种布局的好处是仅在需要时才进行包装。

    【讨论】:

    • 有趣,可以在 RadioGroup 中使用它吗?
    • 我相信它不是,但可以使用 RadioButtons 而无需将它们分配给一个组。
    • @TheRealKingK:如果您将 RadioGroup 子类化并实现类似于 FlowLayout 的 onMeasure() 和 onLayout() 函数,您将获得您正在寻找的解决方案。在此页面上查看我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多