2013-03-15 26 views
16

私は3つのラジオボタンを持っていて、それらを画面全体に均等に配置したいと思います。 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

あなたがそれらを画面幅を共有したい場合も同様に、あなたは、各Viewandroid:layout_width="match_parent"を設定する必要があります。あなたの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は、2つの方法で使用することができます。あなたは垂直線形レイアウトで複数のビューを持っていて、最後の1は、残りのすべてのスペースを取るしたい場合は、wrap_contentにその高さを設定し、最後のビュー1.

  • の重みを与えることができ

    • すべてのビューで使用可能なスペースを共有する場合は、すべての幅/高さを0dpまたはmatch_parentに設定し、各ビューに同じ重み値を与えます。彼らは空間を均等に共有します。

    は、あなたの背景描画可能な規模を持っているように描画可能なこの

    <?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)と基準のように見えるあなたのdrawable/フォルダに入る新しいXMLを作成するにはあなたの背景。

  • +0

    おかげで、それは、仕事しました何らかの理由で 'layout_weight'パラメータが' RadioButtons'を2倍長くします。 http://i.imgur.com/7PZfm1j.png 通常、幅の半分に過ぎません。 – rasen58

    +0

    あなたは 'auton_co1'にスケール可能なビットマップを作ってください。回答はrasen58 @例 –

    +0

    で編集された 'アンドロイド設定:素晴らしいです0dp' – Henry

    0

    は、私は、彼らが確かに各画面の50%を共有した筈のRadioButtonのために使用してレイアウトの重みは(それらは左揃えされた)、それらは、オフセンター整列させることに気づきました。各ラジオボタンのために重力を設定するだけのテキストが/手のひらを顔に当てる中心とする

    を引き起こし以下XMLを水平2つのラジオボタンを整列させる(任意のビューであってもよい)、それらをセンタリングする方法を示しています。

       <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> 
    
    関連する問題