2012-05-05 64 views
11

我正在使用單選按鈕作爲我的應用程序中的選項卡。如何對齊屏幕中央的單選按鈕

我已經爲它加載了圖像,但它們正在朝左側對齊。如何使它們在中心對齊。

enter image description here

這是我的XML文件的外觀

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="20dip" 
     android:background="#fff"/> 

    <RadioGroup android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:checkedButton="@+id/allcontacts" 
     android:id="@+id/contactgroup"> 


     <RadioButton android:id="@+id/allcontacts" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1"/> 

     <RadioButton 
      android:id="@+id/categories" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1"/> 

     <RadioButton android:id="@+id/favourites" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" /> 

    </RadioGroup> 

    <TabWidget android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" android:visibility="gone" /> 
    </LinearLayout> 
</TabHost> 
+0

只是好奇,爲什麼你不是使用TabWidget嗎? – Barak

+0

我希望製表符來到屏幕的底部。如果我使用標籤小部件,標籤看起來如此.. – Naruto

+0

從來沒有想到這一點,好主意。我很高興我問。另一個訣竅填入我的筆記。 :) – Barak

回答

9

在每個RadioButton的xml中嘗試android:gravity="center"

我看到你是wrap_content,所以理論上不應該做任何事情,但看起來像圖標被均勻劃分,也許RadioGroup均勻分配空間,所以使用android:重力會產生效果。

值得一試我猜。

編輯

試試這個:

<RadioButton android:id="@+id/allcontacts" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:marginLeft="20dp" 
    android:gravity="center" 
    android:layout_weight="1"/> 

你將不得不與android:marginLeft="20dp"玩得到它正確居中,但會讓你做你希望做什麼。我複製了你的佈局並且這次測試了,所以我知道它的工作原理! :)

+1

沒有它根本沒有工作:( – Naruto

+0

有另一個想法,更新了我的答案。 – Barak

+0

我試過了,但無法得到預期的結果:( – Naruto

0

嘗試在您RadioGroup添加android:layout_gravity="center_horizontal"

3

你可以把你的單選按鈕中的FrameLayout和中鋒他們:

<RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/gray_percent" 
    android:orientation="horizontal" > 

    <FrameLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 

      android:button="@drawable/rb_temp1" 
      android:checked="true" 
      android:gravity="center" /> 
    </FrameLayout> 

    <FrameLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 

      android:button="@drawable/rb_temp1" 
      android:gravity="center" /> 
    </FrameLayout> 

</RadioGroup> 
+0

但是,你將如何訪問廣播組的屬性來選擇只有一個孩子。 – CoDe

+0

爲了重申@Shubh的問題,如果使用這種對齊單選按鈕的方法,則一次只能選擇一個單選按鈕的默認功能將不起作用。您將不得不手動實現允許一次檢查一個按鈕的處理。 因此,如果這個默認功能對您來說很重要,我建議您找到另一種對中單選按鈕的方法。 – w3bshark

22

我以前也有類似的問題,我看着辦吧,所以我試圖解釋它。

runtime screenshot

以上是我的應用程序截圖,你可以看到,我也做了同樣的,我有一個菜單對齊屏幕底部,他們是一個RadioGroup中和三個RadionButton裏面,我想每個菜單圖標對齊RadionButton的中心,當按鈕檢查事件觸發時,陰影圖像(9-patch)一起顯示。
起初,我用android:按鈕屬性是指一個可繪製的選擇器,選擇器有兩個可繪製的狀態,在檢查和取消選中之間,但我無法對齊菜單圖標中心並使得影子圖像被RadionButton填滿,喜歡它:

enter image description here

我想包括機器人:layout_gravity =中心安卓重力=中心與RadionButton,但它也沒有得到效果。在那之後,我的同事告訴我安卓按鈕屬性指的是前景,而不是背景,然後我用安卓背景代替安卓按鈕和它的工作,下面是我的代碼:

<RadioGroup android:layout_width="match_parent" android:layout_height="60dp" 
     android:orientation="horizontal" android:background="@drawable/menu_bg"> 

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent" 
       android:layout_weight="1" android:button="@null" android:checked="true" 
       android:background="@drawable/menu_item_selector" /> 

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent" 
       android:layout_weight="1" android:button="@null" 
       android:background="@drawable/menu_item_selector" /> 

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent" 
       android:layout_weight="1" android:button="@null" 
       android:background="@drawable/menu_item_selector" /> 

</RadioGroup> 

menu_item_selector是因爲它有重力設置重要的事情:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_item_layer_list" android:state_checked="true" /> 
    <item android:drawable="@drawable/menu_item_layer_list" android:state_pressed="true" /> 
    <item> 
     <bitmap android:src="@drawable/menu_item_off" android:gravity="center" /> 
    </item> 
</selector> 

導致我有兩個圖像繪製,當用戶打開Button狀態檢查,陰影背景圖像和圖標活動狀態圖像,所以我使用層列表來實現它,活動圖標也有重力設置,下面是menu_item_layer_list代碼:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_bg_pressed" /> 
    <item> 
     <bitmap android:src="@drawable/menu_item_on" android:gravity="center" /> 
    </item> 
</layer-list> 

我不知道解決方案是否完美或不是,因爲我總是將它作爲視圖實現並繪製自己。但是我認爲Android SDK提供了更方便的組件讓我們完成了使用xml配置的工作,我們應該學習和使用它。

+1

非常優雅的解決方案。謝謝! – Johan

+1

迄今爲止最好的解決方案!尤其是在您的選擇器的標籤中的android:gravity =「center」thingy對我來說是一件真正的新事物!豎起大拇指! – baske

+1

android:button =「@ null」主要是在這裏! – Yazon2006

13

您可以使用空視圖來均勻地填充單選按鈕之間的空間。 在這種情況下,我有3個單選按鈕,看起來居中,漂亮和整齊。

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

      <View 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 

      <RadioButton 
       android:id="@+id/radio0" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:text="" /> 

      <View 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 

      <RadioButton 
       android:id="@+id/radio1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:checked="true" 
       android:gravity="center" 
       android:text="" /> 

      <View 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 

      <RadioButton 
       android:id="@+id/radio2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:text="" /> 

      <View 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 
     </RadioGroup> 
+1

感謝分享樣品:) – Naruto

+1

很好的解決方案!但我用TextView來代替View和最右和左視圖android:layout_weight =「0.5」。 – Harry

+0

謝謝哈利,那是對的。 – Bolling

0

我在我的RadioGroup中使用了這條線,它工作的很好!

android:gravity="center|left" 
+0

非常感謝,非常感謝分享。 – Naruto

1

我試過所有上述,它不起作用。

我發現了一個更好的解決方案,我想分享一下。 將您的按鈕設置爲@null;然後將該繪圖添加到android:drawableTop或任何其他類型的位置。這將使我們能夠將drawables設置爲radioButton的其他位置。添加一些paddingdrawablePadding,以便它不會粘在按鈕/文本的任何一側。

android:background="@drawable/background_paint" 
android:drawableTop="@drawable/shape" 
android:button="@null" 
+0

你是對的!只是我添加了特定的**高度** + ** android:minHeight ** + ** android:paddingTop **! =中心圖像它看起來不錯!謝謝! – Cristofer

1

在我的情況下,我的單選按鈕內容是對齊的。我讓他們通過設置android:background來對齊中心值,例如@null或某種顏色。下面 是我的佈局

<LinearLayout 
    android:id="@+id/tab" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" > 

    <RadioGroup 
     android:paddingTop="5dp" 
     android:id="@+id/radioGroupAsTab" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:gravity="center_vertical|center_horizontal" 
     android:background="@color/v3_header_bg" 
     android:fadingEdge="none" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/rbDiagnose" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@null" 
      android:button="@null" 
      android:checked="true" 
      android:drawableTop="@drawable/v3_tab_diagnose" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="@string/tabCaption_health_maintenance" 
      android:textColor="@color/radio_astab_changecolor" 
      android:textSize="10sp" 
      android:textStyle="bold" /> 

     <RadioButton 
      android:id="@+id/rbHistory" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@null" 
      android:button="@null" 
      android:drawableTop="@drawable/v3_tab_history" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="@string/tabCaption_history" 
      android:textColor="@color/radio_astab_changecolor" 
      android:textSize="10sp" 
      android:textStyle="bold" /> 

     <RadioButton 
      android:id="@+id/rbChart" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@null" 
      android:button="@null" 
      android:drawableTop="@drawable/v3_tab_chart" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="@string/tabCaption_graph" 
      android:textColor="@color/radio_astab_changecolor" 
      android:textSize="10sp" 
      android:textStyle="bold" /> 

     <RadioButton 
      android:id="@+id/rbCyclopedia" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@null" 
      android:button="@null" 
      android:drawableTop="@drawable/v3_tab_encyclopedia" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="@string/tabCaption_encyclopedia" 
      android:textColor="@color/radio_astab_changecolor" 
      android:textSize="10sp" 
      android:textStyle="bold" /> 

     <RadioButton 
      android:id="@+id/rbSetting" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@null" 
      android:button="@null" 
      android:drawableTop="@drawable/v3_tab_setting" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="@string/tabCaption_info" 
      android:textColor="@color/radio_astab_changecolor" 
      android:textSize="10sp" 
      android:textStyle="bold" /> 
    </RadioGroup> 
</LinearLayout> 
1

認沽安卓重力=上RadioGroup中標記「中心」。這工作。

1
<RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" > 
     <RadioButton 
     android:id="@+id/rb1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="One"/> 

     <RadioButton 
     android:id="@+id/rb2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Two"/> 
</RadioGroup> 
+0

它工作正常......對我來說! –

+0

我認爲這是更好的解決方案。將寬度設置爲'wrap_content'或特定的一致寬度,可以使RadioButtons只有它們所需的寬度,並且RadioGroup可以將整個RadioButton集合居中。做得好! – w3bshark

0

這是在我的情況下對面,我只是把機器人:paddingLeft =「0dp」和它的工作,所以你應該嘗試的android:paddingRight =「0dp」的單選按鈕