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

问题描述 投票:1回答:1

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

   <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的航空感觉来设置单选按钮的风格。由于上面的代码是Radio组功能的装箱问题(错误包括 - 不工作的clearCheck()方法以及多次检查发生..等等)。请帮助我按照我想要的方式设置单选按钮的样式(即给每个单选按钮一张像卡一样的卡片)

android xml android-layout user-interface android-cardview
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.