У меня есть две пользовательские кнопки RadioButton, расположенные рядом, которые заполняют всю ширину экрана. Я хочу, чтобы текст и изображение располагались справа от текста по центру кнопки.
drawableRight рисует изображение далеко справа от кнопки, а не рядом с текстом. Я не могу использовать drawableEnd, потому что у меня нет Android 4.0
Я пробовал использовать Spannable, но проблема в том, что изображение больше текста. Я не хочу, чтобы он был выровнен по нижнему краю или базовой линии текста; Я хочу, чтобы он располагался по центру кнопки.
Лучшее решение, которое я нашел, - это создание интерактивного RelativeLayout с центрированными в нем TextView и ImageView. Это заставляет кнопку выглядеть так, как я хочу, но кажется, что для этого потребуется много работы и запрограммировать каждый RelativeLayout, чтобы он действовал как RadioButton в RadioGroup.
Есть ли более простой способ или что-то, что мне не хватает? Или мне следует сохранить идею RelativeLayout? Вот мой xml для этого:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/first_relativelayout"
android:clickable="true"
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/first_textview"
android:text="@string/first_radiobutton_label"
android:textAppearance="?android:attr/textAppearanceButton"
android:textColor="@android:color/white"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<ImageView
android:contentDescription="@string/image_description"
android:id="@+id/first_imageview"
android:layout_centerVertical="true"
android:src="@drawable/it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/first_textview">
</ImageView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/second_relativelayout"
android:clickable="true"
android:background="@drawable/radio_button"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="@+id/second_textview"
android:text="@string/second_radiobutton_label"
android:textAppearance="?android:attr/textAppearanceButton"
android:textColor="@android:color/white"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<ImageView
android:contentDescription="@string/image_description"
android:id="@+id/second_imageview"
android:layout_centerVertical="true"
android:src="@drawable/it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/second_textview">
</ImageView>
</RelativeLayout>
</LinearLayout>