我想在文本的右側製作兩個大小相同的單選按鈕,其中包含自定義背景,文本和圖像。由於這些與標準的「按鈕」有多麼不同,我使用可點擊的「RelativeLayout」來製作它們。爲什麼我的「居中」對象在我的RelativeLayout中稍微偏離中心?
文字和圖像的高度不同,但我希望每個文字和圖像在按鈕中垂直居中。我還希望文本+圖像的組合在按鈕中水平居中。第二部分是我遇到的麻煩;它偏離中心,靠近左側。在下圖中,左側是我想要的,但右側是發生了什麼。其中一個按鈕(具有較長文本的按鈕)上的圖像也被調整爲較小,儘管按鈕右側仍有足夠的空間。
這裏是我的代碼:
<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>
我在做什麼錯?
這並沒有改變任何東西,但謝謝。我在那裏有一個希望。我之所以不使用drawableRight,是因爲它將它繪製在按鈕的右邊緣,而不是立即在文本的右邊。 – Kalina
你可以在你的問題中更新你現在使用的佈局嗎? –
@TheBeatlemaniac編輯回答。 –