【问题标题】:Stylizing the radio buttons Using cardview in android在 android 中使用 cardview 样式化单选按钮
【发布时间】:2018-02-24 12:21:51
【问题描述】:

我在单选组中使用单选按钮 - 效果很好

   <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>

但是,每当我尝试通过在卡片视图周围环绕单选按钮来使用某些样式时-单选组失去了所有功能.. 我说的是我使用以下代码时的情况-

 <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() 方法无效以及多次检查发生..等)。 请帮助我以我想要的方式设置单选按钮的样式(即为每个单选按钮提供卡片般的感觉)

【问题讨论】:

  • 而不是在卡片视图中放置单选按钮尝试为您提供单选按钮的背景
  • 当他们是radiogroup的时候不要在cardview里面添加radiobutton。看看如何自定义radio button。

标签: android xml android-layout user-interface android-cardview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2023-04-09
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多