2015-02-06 180 views
0

我在一個下面的radiogroup中使用兩個單選按鈕。上面的按鈕有文字「to」,另一個有「from」。我想要的是,兩者都將在中心,但他們將由他們的checkradio對齊。我嘗試這樣做:通過checkradio對齊單選按鈕

<RadioGroup 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/rgroup_pk" 
       android:gravity="center"> 

       <RadioButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/rbtn_to" 
        android:text="@string/title_to" 
        android:checked="true" 
        android:gravity="center|center_vertical" /> 


       <RadioButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/rbtn_from" 
        android:text="@string/title_from" 
        android:gravity="center|center_vertical" /> 

      </RadioGroup> 

這使得按鈕停留在垂直和水平中心,但由於他們的文字有不同的大小是什麼顯示的是這個(如果「x」是checkradio):

    X to 
        X from 

而我想是這樣的:

    x to 
        x from 
+0

嘗試改變'安卓重力=「中心| center_vertical」''到安卓重力=「左」' – 2015-02-06 09:09:28

回答

0

中包括一個RelativeLayout的單選按鈕的。

<RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/rgroup_pk" 
     android:gravity="center"> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center|center_vertical"> 

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

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/rbtn_from" 
       android:text="FROM" 
       android:layout_below="@+id/rbtn_to"/> 
     </RelativeLayout> 
    </RadioGroup> 

請注意android:layout_below="@+id/rbtn_to"。正如名字所暗示的那樣,第二個RadioButton放置在第一個RadioButton的下面,並且不像在RelativeLayout中正常那樣重疊。

0
Use this code using RelativeLayout. 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<RadioGroup 
    android:id="@+id/rgroup_pk" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" > 

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

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

</RelativeLayout> 
+0

它把左邊兩個按鈕,它們對齊,但不是在水平中心 – Ilenca 2015-02-06 09:41:59