【发布时间】:2016-08-22 12:06:49
【问题描述】:
我想要一个RadioGroup,并在两列中显示各种RadioButtons。但似乎RadioGroup 强制RadioButtons 仅显示在一列中。有没有办法让按钮显示在两列中?
例如,这个 xml 代码会将四个按钮放在 square form 中,但在 RadioGroup 中它们都在 one column 中。
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio1"
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/radio2"
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="42dp"
android:layout_marginEnd="42dp"
android:layout_alignBottom="@+id/radio1"/>
<RadioButton
android:id="@+id/radio3"
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radio1"
android:layout_alignLeft="@+id/radio1"
android:layout_alignStart="@+id/radio1"
android:layout_marginTop="34dp"/>
<RadioButton
android:id="@+id/radio4"
android:text="Button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radio3"
android:layout_alignLeft="@+id/radio2"
android:layout_alignStart="@+id/radio2"/>
</RadioGroup>
【问题讨论】:
-
RadioGroup 扩展了 LinearLayout,所以这根本无法做到。您可以覆盖 RelativeLayout 并在其中执行一些检查/取消检查逻辑。
-
@convexHull 我怎么能覆盖它,并将这些按钮作为
square form? -
检查这个developer.android.com/training/custom-views/index.html 创建一些类(例如RelativeRadioGroup)并让它扩展RelativeLayout。查看 RadioGroup 的源代码,了解如何实现按钮检查/取消检查的附加逻辑
标签: android xml android-layout android-radiogroup android-radiobutton