我想制作两个具有自定义背景、文本和文本右侧图像的大小相同的单选按钮。由于它们与标准“按钮”有多么不同,我使用可点击的“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>
我究竟做错了什么?