安卓单选按钮组布局

3

你好,我希望创建一个包含21个按钮的RadioGroup,但它们不能在一行或一列中排列,应该像7x3那样。问题在于,我尝试过不使用RadioGroup,效果还不错。我使用了以下XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/darbu_pusl"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayJobsPage" >

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="330dp"
    android:background="@drawable/job_btn_pi"
    android:gravity="left" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/radioButton1"
    android:layout_marginLeft="23dp"
    android:layout_toRightOf="@+id/radioButton1"
    android:background="@drawable/job_btn_a"
    android:gravity="left" />

但是现在当我像这样使用RadioGroup时:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/darbu_pusl"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayJobsPage">
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="330dp"
    android:background="@drawable/job_btn_pi"
    android:gravity="left" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/radioButton1"
    android:layout_marginLeft="23dp"
    android:layout_toRightOf="@+id/radioButton1"
    android:background="@drawable/job_btn_a"
    android:gravity="left" />

我收到了关于layout_alignTop和layout_toRightOf无效的布局参数的警告。你们能帮我解决这个问题吗?在RadioGroup中是否可以复制第一个布局选项?

1个回答

1

将您的 RelativeLayout 包装在一个 RadioGroup 中使用。


谢谢,那个可以正常工作。我可以输入我想要的订单,但是主要问题仍然存在,我无法检查几个按钮。同时还收到警告:“此RelativeLayout布局或其RadioGroup父项是无用的;将背景属性转移到其他视图”,已更改为以下代码。 - Shien
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/darbu_pusl" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"> - Shien
好的,这个不起作用。RadioGroup使用LinearLayout,它不会自动换行。由于单选按钮必须是单选组的直接子项,因此您无法将子布局添加到单选组中。 - ramaral
那么唯一的选择就是<linearlayout><radiogroup></radiogroup></linearlayout>吗?那么有没有办法控制其他单选按钮组中的点击呢?我的意思是,如果在一个单选按钮组中选择了一个按钮,然后在另一个单选按钮组中单击了一个按钮,那么该按钮将被选中,之前的按钮将不再选中。我希望你能理解。 - Shien
不,RadioGroup本身就是一个LinearLayout。唯一的方法是使用单独的单选按钮,然后通过编程控制它们的开/关状态。 - ramaral
非常感谢,我设法想出了一个解决方法,它将使用3个单选按钮组。这可能不是正确的选择,但它可能会起作用:D 再次感谢。 - Shien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接