在 android 中使用 cardview 对单选按钮进行样式化

Stylizing the radio buttons Using cardview in android

我在单选组中使用单选按钮 - 它完美无缺

   <RadioGroup
    android:layout_below="@id/textview_question"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="30dp"
    android:id="@+id/radgrp">
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:textSize="20dp"
        android:layout_weight="1"
        android:id="@+id/radiobutton_1"
        android:text="option"
        android:textStyle="italic"/>


    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:textSize="20dp"
        android:layout_weight="1"
        android:id="@+id/radiobutton_2"
        android:text="option"
        android:textStyle="italic"/>

    <RadioButton
        android:id="@+id/radiobutton_3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:text="option"
        android:textSize="20dp"
        android:textStyle="italic"
        />
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textSize="20dp"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:id="@+id/radiobutton_4"
        android:text="option"/>

</RadioGroup>

但是每当我尝试通过用 cardview 包围单选按钮来使用一些样式时,单选按钮组就会失去所有功能.. 我说的是我使用下面代码时的情况-

 <RadioGroup
    android:layout_below="@id/textview_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp"
android:id="@+id/radgrp">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="20dp"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginBottom="10dp">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<RadioButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:textSize="20dp"
    android:layout_weight="1"
    android:id="@+id/radiobutton_1"
    android:text="option"
    android:textStyle="italic"/>
    </LinearLayout>
</android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="10dp"
        app:cardCornerRadius="20dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<RadioButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:textSize="20dp"
    android:layout_weight="1"
    android:id="@+id/radiobutton_2"
    android:text="option"
    android:textStyle="italic"/>
    </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="10dp"
        app:cardCornerRadius="20dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <RadioButton
        android:id="@+id/radiobutton_3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:text="option"
        android:textSize="20dp"
        android:textStyle="italic"
        />
    </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="10dp"
        app:cardCornerRadius="20dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<RadioButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="20dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:id="@+id/radiobutton_4"
    android:text="option"/>
    </LinearLayout>
    </android.support.v7.widget.CardView>
</RadioGroup>

现在的问题是如何通过让单选按钮具有像 cardview 一样的空气感来设计单选按钮的样式。由于上面的代码在单选组的功能中出现了问题(错误包括 - [= 的不工作13=] 方法和多重检查发生..等)。 请帮助我以我想要的方式设置单选按钮的样式(即给每个单选按钮一个卡片般的感觉)

在不同 cardview 布局中使用相同单选组的不同单选按钮将不起作用。所以你的做法是错误的。相反,您可以尝试将 textview 与单个单选按钮一起放置。在 Java 代码中,您可以检查选中了哪个 甚至禁用其他单选按钮。 并且这只是按照您的方法实现目标的最简单方法。