2

我有两个并排的自定义 RadioButtons 填充屏幕的宽度。我想在文本右侧有文本和图像,以按钮为中心。

drawableRight 将图像绘制到按钮右侧,而不是靠近文本。我不能使用 drawableEnd 因为我没有 Android 4.0

我试过使用 Spannable,但问题是图像比文本大。我不希望它与文本的底部或基线对齐;我希望它自己居中在按钮中。

我发现的最佳解决方案是制作一个可点击的 RelativeLayout,其中 TextView 和 ImageView 居中。这使按钮看起来像我想要的那样,但要做到这一点似乎需要做很多工作,并且要对每个 RelativeLayout 进行编程,使其像 RadioGroup 中的 RadioButton 一样工作。

有没有更简单的方法,或者我错过了什么?还是我应该保留我的 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>
4

1 回答 1

0

我继续使用RelativeLayout 解决方案。它需要的工作比看起来应该值得的要多,但它工作得很好。

于 2012-08-15T14:38:48.423 回答