【问题标题】:How to align radiobuttons in a radiogroup如何对齐单选按钮组中的单选按钮
【发布时间】:2015-12-27 01:51:39
【问题描述】:

我有一个单选组,其中有 4 个单选按钮。我希望它们对齐为两条水平线,即水平的两个单选按钮和水平的其他两个单选按钮,位于第一个水平单选按钮的下方。

如何在不改变其功能的情况下完成此操作

这意味着只能选择一个单选按钮。

这是我的简单代码:

<RadioGroup
    android:id="@+id/radiogroup_relation_options"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/HeadingText"
    android:background="#4269c6" >

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/radiobutton_friend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="Friend"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/radiobutton_business"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="We&apos;ve done Business Together"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/radiobutton_colleague"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="Colleague"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/radiobutton_other"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="Other"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>
</RadioGroup>

【问题讨论】:

  • 使用android:weight
  • 请附上您的 XML 代码。我会编辑它并给你解决方案
  • 嘿,看看我编辑的代码

标签: android


【解决方案1】:

这是另一个简单的解决方案。

ma​​in_activity.xml

<LinearLayout 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:orientation="vertical"
  tools:context=".MainActivity">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/one_radio_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="one" />

    <RadioButton
        android:id="@+id/two_radio_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="two" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/three_radio_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="three" />

    <RadioButton
        android:id="@+id/four_radio_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="four" />
   </LinearLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  private RadioButton mOneRadioButton;
  private RadioButton mTwoRadioButton;
  private RadioButton mThreeRadioButton;
  private RadioButton mFourRadioButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mOneRadioButton = (RadioButton) findViewById(R.id.one_radio_btn);
    mTwoRadioButton = (RadioButton) findViewById(R.id.two_radio_btn);
    mThreeRadioButton = (RadioButton) findViewById(R.id.three_radio_btn);
    mFourRadioButton = (RadioButton) findViewById(R.id.four_radio_btn);

    mOneRadioButton.setOnClickListener(this);
    mTwoRadioButton.setOnClickListener(this);
    mThreeRadioButton.setOnClickListener(this);
    mFourRadioButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.one_radio_btn:
            mOneRadioButton.setChecked(true);
            mTwoRadioButton.setChecked(false);
            mThreeRadioButton.setChecked(false);
            mFourRadioButton.setChecked(false);

            break;
        case R.id.two_radio_btn:

            mOneRadioButton.setChecked(false);
            mTwoRadioButton.setChecked(true);
            mThreeRadioButton.setChecked(false);
            mFourRadioButton.setChecked(false);
            break;
        case R.id.three_radio_btn:

            mOneRadioButton.setChecked(false);
            mTwoRadioButton.setChecked(false);
            mThreeRadioButton.setChecked(true);
            mFourRadioButton.setChecked(false);
            break;
        case R.id.four_radio_btn:

            mOneRadioButton.setChecked(true);
            mTwoRadioButton.setChecked(false);
            mThreeRadioButton.setChecked(false);
            mFourRadioButton.setChecked(true);
            break;

    }
  }
}

【讨论】:

  • 这是一个不错的解决方案,但它非常冗长。谢谢您的解决方案
【解决方案2】:

RadioGroup 是 LinearLayout 的子类,如果你想让它支持多行,则需要覆盖它,但现在有这样的小部件可以使用:MultiLineRadioGroup,帮助示例可以帮助你。

【讨论】:

  • 我不知道。感谢分享如此有价值的信息。
【解决方案3】:

用两种不同的 LinearLayout 试试:

<RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

    </RadioGroup>

【讨论】:

  • 我已经试过了。在radiogroups之间放置线性布局会覆盖radiogroup的功能。
【解决方案4】:

试试这个,告诉我它是否符合要求。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<RadioGroup
    android:id="@+id/radiogroup_relation_options"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/HeadingText"
    android:background="#4269c6" >

    <!-- 2 columns -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

      <RadioButton
                android:id="@+id/radiobutton_friend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="Friend"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/radiobutton_business"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="We&apos;ve done Business Together"
                android:textSize="15sp" />
    </TableRow>

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
<RadioButton
                android:id="@+id/radiobutton_colleague"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="Colleague"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/radiobutton_other"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:button="@drawable/custom_radiobutton"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="Other"
                android:textSize="15sp" />
    </TableRow>
</RadioGroup>

</TableLayout>

我这里使用了表格布局,所以有 2 行 RadioButtons。

【讨论】:

  • 这个我也试过了。设置单选按钮没什么大问题。问题是功能。这种方式覆盖了单选按钮的功能。即一次只能选择一个单选按钮。顺便说一句!感谢您的努力和时间
  • 我做了小调整,复制粘贴,请告诉我,即使你试过了,请再试一次
【解决方案5】:

您可以通过使用 2 个 radioGroups 来实现此目的

  1. android:orientation = "horizontal"创建RadioGroup
  2. 在其中放入 2 个单选按钮。
  3. 创建另一个RadioGroup,同上。
  4. 确保RadioGroupsRadioButtons 的ID 不同。
  5. 使用setOnCheckedChangeListener 在两个组上设置相同的OnCheckedChangeListener

    //assuming activity implements OnCheckedChangeListener rg1.setOnCheckedChangeListener(this); rg2.setOnCheckedChangeListener(this);

  6. 在其他无线电组上调用clearCheck(如果已在组 2 中执行检查,则为组 1)

  7. 在执行操作时检查两个组是否有选中的RadioButton

【讨论】:

  • @Abhay 我为第 5 步添加了 sn-p
猜你喜欢
  • 2017-01-16
  • 1970-01-01
  • 2017-01-10
  • 2014-03-17
  • 2011-05-12
  • 2021-01-20
  • 2017-03-09
  • 2018-01-16
  • 2016-06-09
相关资源
最近更新 更多