如何将图像放置在 android 中的单选组中以及 android 中的单选按钮

How to place images in radio group in android along with radio button in android

我想设计带有相应文本的单选按钮,images.when我尝试在单选组内使用线性布局,所有单选按钮都是enabled.how可以解决这个问题

这是在单选组中使用图像和单选按钮的方法

<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <RadioButton
            android:id="@+id/radio0"
            android:layout_margin="10dp"
            android:drawableRight="@drawable/ic_launcher_background"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <RadioButton
            android:id="@+id/radio1"
            android:layout_margin="10dp"
            android:drawableRight="@drawable/ic_launcher_background"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</RadioGroup>

I have designed the same layout as below. It might help you.

                <RadioButton
                    android:id="@+id/radioSubject"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:button="@android:color/transparent"
                    android:drawableTop="@drawable/subject_selector"
                    android:fontFamily="@font/roboto_bold"
                    android:gravity="center"
                    android:text="@string/txt_subject"
                    android:textColor="@color/radio_text_selector"
                    android:textSize="@dimen/_15ssp" />

                <Space
                    android:layout_width="@dimen/_10sdp"
                    android:layout_height="match_parent" />

                <RadioButton
                    android:id="@+id/radioChapter"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:button="@android:color/transparent"
                    android:drawableTop="@drawable/chapter_selector"
                    android:fontFamily="@font/roboto_bold"
                    android:gravity="center"
                    android:text="@string/txt_chapter"
                    android:textColor="@color/radio_text_selector"
                    android:textSize="@dimen/_15ssp" />

                <Space
                    android:layout_width="@dimen/_10sdp"
                    android:layout_height="match_parent" />

                <RadioButton
                    android:id="@+id/radioTopic"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:button="@android:color/transparent"
                    android:drawableTop="@drawable/topic_selector"
                    android:fontFamily="@font/roboto_bold"
                    android:gravity="center"
                    android:text="@string/txt_topic"
                    android:textColor="@color/radio_text_selector"
                    android:textSize="@dimen/_15ssp" />
            </RadioGroup>

这里是subject_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_ico_subject_active" android:state_checked="true" />
    <item android:drawable="@drawable/ic_ico_subject" />
</selector>

您必须为其他 RadioButton 创建相同的 xml

如果您想更改所选 RadioButton 的文本颜色,那么这里是 xml。将此 xml 文件放入 res>color>.

这里是 radio_text_selector.xml 的文字颜色。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/colorPrimary"/>
    <item android:state_checked="true" android:color="@color/colorPrimary"/>
    <item android:color="@color/dividerColor"/>
</selector>

这里是输出看起来像