Android按钮背景形状

标签 android radio-button android-ui android-styles

我想创建一个如下所示的单选按钮:

enter image description here

也就是说,当被选中时,会出现一个复选标记以指示它已被选中,而不是一个点。未选中时,只有橙色背景(或其他颜色)。

页面的黑色背景只是应用程序的背景。

我知道我可以为每个按钮的不同状态创建不同的可绘制对象,但如果有很多颜色,这就不是真正可扩展的了。

我在想我可以为 android:button 属性添加一个复选标记图像作为可绘制对象,并为单选按钮的 android:background 提供不同的颜色。但效果不佳,因为背景的形状总是方形的。所以我最终得到了一个方形按钮。

实现这种按钮的最佳方式是什么?或者,如何将背景形状更改为圆形/椭圆形而不是方形?

这是我现在拥有的:

<!-- layout.xml -->
<RadioButton
    android:id="@+id/radio1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@color/orange"
    android:button="@drawable/radio"/>

<!-- drawable/radio.xml -->
<item 
    android:state_checked="true" 
    android:drawable="@drawable/checkmark_on_oval"
    >
</item>    

最佳答案

试试这个可能对你有帮助。 将 radio_button 属性设置为

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@null"
        android:checked="true"
        android:drawableLeft="@drawable/radio_button_selector"
        android:drawablePadding="5dp"
        android:paddingLeft="2dp" />

在drawable文件夹中放rb_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radio_button_unchecked" android:state_checkable="true"/> <!-- pressed -->
    <item android:drawable="@drawable/radio_button_checked" android:state_checked="true"/> <!-- focused -->
    <item android:drawable="@drawable/radio_button_unchecked"/> <!-- default -->

</selector>

此处 radio_button_uncheckedradio_button_checked 是您的单选按钮的两个图像。

关于Android按钮背景形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22968987/

相关文章:

android - 如何在android中为EditText添加边框

android - 如何修复Android项目构建上的重复类

delphi - Delphi 2010 中 RadioGroup.ControlCount 的问题

android - DatePicker 的 setMinDate(...) 在第二次调用时不起作用

android - 修改 Button 时 onResume 出现 NullPointerException,为什么?

android - 从 ICS ActionBar 切换到 Lollipop 工具栏后缺少导航图标

android - SQLite:删除大量行的有效方法

java - 为什么我的随机数组没有在每次渲染上绘制随机纹理?我必须改变什么?

html - 如何更改html中单选按钮的位置?

php - 使用单选按钮更新记录