使用自定义选择器时如何移除默认单选按钮复选框

13

我想给一个单选框加样式,所以创建了一个选择器。所有的样式都生效了,但是默认的单选框图标还在显示。如何去除默认的单选框复选框?我尝试将 drawable 赋值为空字符串,但编译器不允许这样做。

以下是一个 selector 状态的代码:

<item android:state_checked="true" android:drawable="">
    <shape>
        <gradient
            android:startColor="#808080"
            android:centerColor="#2F2F2F"
            android:endColor="#2F2F2F"
            android:angle="270" />
        <stroke
            android:width="2dp"
            android:color="@color/black" />
        <corners
            android:radius="5dp" />
    </shape>
</item>

1
你为什么要完全删除这个可绘制对象?你能否用适合你需求的东西来替换它呢? - Flynn
我想要一个单选按钮的功能,但是希望它看起来像一个按钮。具体来说,我想要两个并排的按钮,可以相互切换。其中一个按钮向下,另一个向上。除了我目前的方向外,是否有更好的解决方案? - Kevin Westwood
2
我实际上发现,我可以使用以下代码android:button="@null"来移除默认的复选框。 - Kevin Westwood
3个回答

41

在布局XML中将按钮值设置为null

android:button="@null"

3
谢谢你的回答。我也已经发现了,正如你可以从我的2012年2月15日在问题中看到的评论一样。 - Kevin Westwood
2
为了兼容 Android 5 及以下版本,还需添加 app:buttonCompat="@null"。 - Hamed Jaliliani
@HamedJaliliani,你的评论对我的事业非常有帮助。非常感谢。 - Joe Okatch

24
我发现无法在选择器中删除默认的单选按钮复选框。我发现可以通过将"button"属性分配为null来删除它,无论是在样式还是单选按钮本身的定义中。
以下是使用样式的示例。
<RadioButton
    android:id="@+id/myRadioButton"
    style="@style/RadioButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/custom_radio_button"/>

<style name="RadioButtonStyle" parent="@android:style/Widget.CompoundButton">
    <item name="android:background">@drawable/radiobutton_selector</item>
    <item name="android:button">@null</item>
</style>

radiobutton_selector 定义了一个选择器,用于绘制不同选中状态下的按钮。这是选中状态:

<item android:state_checked="true">
    <shape>
        <gradient
            android:startColor="@color/titleButtonCheckedGradientLight"
            android:centerColor="@color/titleButtonCheckedGradientDark"
            android:endColor="@color/titleButtonCheckedGradientDark"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="@color/titleButtonBorder" />
        <corners
            android:radius="5dp" />
    </shape>
</item>

您的代码似乎无法使用radioGroup.getCheckedRadioButtonId()函数…… - Prativa
目前为止最佳答案 - Ashton

0
如果您想以编程方式设置RadioButton的自定义背景,则可以像这样设置:
radioButton.setButtonDrawable(null);
radioButton.setBackgroundResource(R.drawable.your_custom_drawable);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接