我在单选组中使用单选按钮 - 它工作得很好
<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 一样的空气感来设计单选按钮的样式。由于上面的代码在单选组的功能中产生了问题(错误包括 - clearCheck 不工作()
方法以及发生的多重检查等)。
请帮助我以我想要的方式设置单选按钮的样式(即给每个单选按钮一个卡片般的感觉)
最佳答案
在不同的 cardview
布局中使用相同单选组的不同单选按钮将不起作用。所以你的做法是错误的。相反,您可以尝试将 textview
与单个单选按钮一起放置。在 Java 代码中,您可以检查选中了哪个按钮,甚至可以禁用其他单选按钮。 而且这只是按照您的方法实现目标的最简单方法。
关于android - 样式化单选按钮 在 android 中使用 cardview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48962815/