2012-08-30 45 views
1

我想在文本的右側製作兩個大小相同的單選按鈕,其中包含自定義背景,文本和圖像。由於這些與標準的「按鈕」有多麼不同,我使用可點擊的「RelativeLayout」來製作它們。爲什麼我的「居中」對象在我的RelativeLayout中稍微偏離中心?

文字和圖像的高度不同,但我希望每個文字和圖像在按鈕中垂直居中。我還希望文本+圖像的組合在按鈕中水平居中。第二部分是我遇到的麻煩;它偏離中心,靠近左側。在下圖中,左側是我想要的,但右側是發生了什麼。其中一個按鈕(具有較長文本的按鈕)上的圖像也被調整爲較小,儘管按鈕右側仍有足夠的空間。

What I want is on the left, and what's happening is on the right.

這裏是我的代碼:

<LinearLayout 
    android:baselineAligned="false" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <RelativeLayout 
     android:id="@+id/my_button" 
     android:background="@drawable/radio_button" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 
     <TextView 
      android:id="@+id/button_textview" 
      android:text="@string/button_label" 
      android:textAppearance="?android:attr/textAppearanceButton" 
      android:layout_centerVertical="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"></TextView> 
     <ImageView 
      android:contentDescription="@string/image_description" 
      android:id="@+id/button_imageview" 
      android:layout_centerVertical="true" 
      android:src="@drawable/my_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/button_textview"> 
     </ImageView> 
    </RelativeLayout> 

    <RelativeLayout 
     ... same thing for the second button ... 
    </RelativeLayout> 

</LinearLayout> 

我在做什麼錯?

回答

0

原來溶液中加入

android:paddingRight="0dip" 

奇怪的是,儘管我沒有在那裏放任何填充。

1

使用此爲您的按鈕:

<LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:layout_gravity="center_horizontal" 
       > 
    <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:text="MyButton"/> 
    <ImageView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:src="@drawable/my_image"/> 

</LinearLayout> 

現在,你可以將其放置在其他家長的意見。要將佈局屬性應用於上述按鈕,請將這些屬性放置在上面按鈕的外部<LinearLayout>標記中。

備選:

您可以設置一個TextView使用屬性,如對邊(左,右,上,下),要繪製的自定義圖像: android:drawableLeft="@drawable/my_image"

+0

這並沒有改變任何東西,但謝謝。我在那裏有一個希望。我之所以不使用drawableRight,是因爲它將它繪製在按鈕的右邊緣,而不是立即在文本的右邊。 – Kalina

+0

你可以在你的問題中更新你現在使用的佈局嗎? –

+0

@TheBeatlemaniac編輯回答。 –