单选按钮,图像作为选项而不是文本

Coo*_*ler 18 android

如何在Android应用程序中添加单选按钮作为图像选项,而不是文本..我们可以通过android:setText属性将文本设置为单选按钮.但我希望在那里显示图像而不是文本..请帮助我?

Jos*_*raj 34

这个使用房产android:drawableRight怎么样?

<RadioGroup
    android:id="@+id/maptype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/line1"
    android:orientation="horizontal" >
    <RadioButton
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:drawableRight="@drawable/map" />

    <RadioButton
        android:id="@+id/satellite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/sat" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

  • 如果图像是从web/url同步的呢? (5认同)

小智 15

用这个:

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,将RadioButton放入LinearLayout会导致RadioGroup停止管理嵌套按钮的已检查状态.因此RadioGroup现在没用了.考虑使用<RadioButton android:drawableRight ="@ drawable/ic_launcher">这避免了对LinearLayout的需要,而RadioGroup可以在管理嵌套的RadioButtons中完成它的工作. (47认同)