2013-03-15 112 views
16

我有三个单选按钮,我想均匀地将它们放在屏幕上。当我使用android:layout_weight="1"时,按钮在屏幕上延伸出来。那么我怎么能在它们之间有相同数量的空间,这些空间也可以在不同的屏幕尺寸上进行缩放?如何在Android中均匀分隔出单选按钮?

<RadioGroup 
     android:id="@+id/auton_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="10dp" 
     android:layout_below="@id/clear_fields" 
       > 
     <RadioButton 
      android:id="@+id/auton_radio_1" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:background="@drawable/auton_col" 
      android:layout_weight="1" 

      /> 
     <!-- android:layout_marginRight="380dp" --> 
     <RadioButton 
      android:id="@+id/auton_radio_2" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:background="@drawable/auton_col" 
      android:layout_weight="1" 


      /> 
     <RadioButton 
      android:id="@+id/auton_radio_3" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:background="@drawable/auton_col" 
      android:layout_weight="1" 
      /> 

    </RadioGroup> 

回答

30

如果你想让它们共享屏幕的宽度同样需要设置android:layout_width="match_parent"每个View。你的XML将成为:

<RadioGroup 
    android:id="@+id/auton_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/clear_fields" 
    android:orientation="horizontal" 
    android:paddingLeft="10dp" > 

    <RadioButton 
     android:id="@+id/auton_radio_1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/auton_col" /> 
    <!-- android:layout_marginRight="380dp" --> 

    <RadioButton 
     android:id="@+id/auton_radio_2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/auton_col" /> 

    <RadioButton 
     android:id="@+id/auton_radio_3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/auton_col" /> 
</RadioGroup> 

为了详细说明,layout_weight可以以两种方式使用。

  • 如果你有一个垂直线性布局多个视图,你想最后一个占用所有剩余空间,你可以设置自己的高度,以wrap_content并给出了最后一个视图1.
  • 的重量如果您希望所有视图共享可用空间,请将所有宽度/高度设置为0dpmatch_parent,并为每个视图指定相同的权重值。他们将平等分享空间。

要让你的背景绘制的规模,使一个新的XML进去你drawable/文件夹,看起来像这样

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="center" 
    android:src="@drawable/auton_col" /> 

是绘制为它命名为任何你喜欢的(如auton_col_scale.xml)和参考你的背景。

+0

谢谢,它的工作,但由于某些原因'layout_weight'参数使我的RadioButtons长两倍。 http://i.imgur.com/7PZfm1j.png 它们通常只有这一宽度的一半。 – rasen58 2013-03-15 23:16:10

+0

你应该使'auton_co1'成为一个可缩放的位图。答案是用示例编辑 – 2013-03-16 05:29:44

+0

@ rasen58将单选按钮的'android:layout_width'设置为'0dp' – Henry 2013-03-16 05:34:29

0

我注意到使用RadioButton的布局权重导致它们偏离了中心,尽管它们肯定每个都共享了50%的屏幕(它们是左对齐的)。设置重力每个单选按钮造成只有文本为中心/捂脸

下面的XML展示了如何水平对齐两个单选按钮(可以是任何意见)和中锋他们:

   <RadioGroup 
        android:id="@+id/radiogroup" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <Space 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 

        <RadioButton 
         android:id="@+id/radioyes" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/yes" 
         android:checked="false"/> 

        <Space 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 

        <RadioButton 
         android:id="@+id/radiono" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/no" 
         android:checked="false"/> 

        <Space 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 

       </RadioGroup>