如何在Android中将图像放置在单选按钮右侧

15

我正在尝试制作一个包含4个选项的单选按钮列表。每个选项旁边需要有文本,但文本右侧也需要有一张图片来描述该选项,而该图片并不是单选按钮本身。

到目前为止我没有成功,设计师似乎无法将事物放在正确位置。你需要知道如何在XML中进行编码。我的XML代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:weightSum="1" android:baselineAligned="true" android:orientation="horizontal">
    <RadioGroup android:layout_weight="0.50" android:layout_height="wrap_content" android:id="@+id/radioGroup1" android:layout_width="0dp">
        <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:checked="true" android:text="A" android:id="@+id/radio0"></RadioButton>
        <ImageView android:layout_toRightOf="@id/radio0" android:layout_height="wrap_content" android:src="@drawable/A2" android:layout_width="wrap_content" android:id="@+id/imageView1"></ImageView>
        <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="B" android:id="@+id/radio1"></RadioButton>
        <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="C" android:id="@+id/radio2"></RadioButton>
        <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="D" android:id="@+id/radio3"></RadioButton>
    </RadioGroup>

</LinearLayout>

我想将一个图片放在第一个单选按钮的右侧,但没有成功。如何让图片在单选按钮右侧位置显示?

3个回答

22
对于一个按钮,你可以将可绘制对象放置在其顶部/右侧/左侧/底部。所以我认为单选按钮可能也是同样的工作原理。
请查看Android开发者网站上的此链接
你试过这个吗?
更新
因此,在您的情况下,请删除ImageView并在单选按钮中添加android:drawableRight(或left/bottom/top)。
<RadioButton
    android:drawableRight="@drawable/A2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="A"
    android:id="@+id/radio0"/>

2
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_launcher"/>

RadioButton默认包含一个TextView,因此您可以执行任何TextView所具有的所有操作。

奖励提示:如果您想更改显示选中状态的圆点,请更新android:button标签以引用自己的可绘制对象。


1
在您的视图中使用android:drawableXXXXX来添加图像/可绘制,如果您想要添加一些填充,则使用可绘制填充。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接