2016-07-22 121 views
0

我需要对齐RadioGroup中的RadioButton,就像RelativeLayout一样。我读过RadioGroup是从LinearLayout继承的,可能我不能像内部的RelativeLayout那样对齐内容。我试图实现的实际内容是RadioGroup中的两行,第一行包含两个RadioButton,在第二行中,我必须在其开始处添加另一个按钮。我怎么能这样做?我如何对齐radiogroup android内的单选按钮?

回答

0

你可以尝试这种布局,让我知道,如果这对你有用。如果需要,我可以修改。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

<RadioGroup 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="5dp"> 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Red"/> 

     <RadioButton 
      android:id="@+id/radioButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Blue"/> 
    </LinearLayout> 

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

     <RadioButton 
      android:id="@+id/radioButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Green"/> 

    </LinearLayout> 
</RadioGroup> 

</LinearLayout> 

Layout screenshot

+0

无法正常工作。对齐是好的。但它不会表现为一个无线电组。我可以选择多个按钮。 –

1

所有你需要做的是设置在无线电集团定向横向到它们对齐水平看看下面的代码。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:padding="@dimen/activity_horizontal_margin"> 


    <RadioGroup 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="match_parent"> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New RadioButton" 
      android:id="@+id/radioButton" /> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New RadioButton" 
      android:id="@+id/radioButton2" /> 
    </RadioGroup> 
    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New RadioButton" 
     android:layout_gravity="start" 
     android:id="@+id/radioButton3" /> 

</LinearLayout>