欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android定制RadioButton样式三种实现方法

程序员文章站 2023-11-30 23:35:16
三种方法 1.使用xml文件进行定义 res/drawable/radio.xml 复制代码 代码如下:
三种方法
1.使用xml文件进行定义 res/drawable/radio.xml
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 未选中->
<item
android:state_checked="false"
android:drawable="@drawable/tabswitcher_long" />
<!--选中->
<item
android:state_checked="true"
android:drawable="@drawable/tabswitcher_short" />
</selector>

在布局文件中使用
复制代码 代码如下:

<radiogroup
...
>
<radiobutton
...
android:button="@null"
android:background="@drawable/radio"
/>
</radiogroup>

ndroid:button="@null" 去除radiobutton前面的圆点
android:background="@drawable/radio" 使用定义的样式
2.在java代码中定义
复制代码 代码如下:

@override
public boolean ontouchevent(motionevent event) {
if(event.getactionmasked() == motionevent.action_down){
this.setbackgroundresource(com.wxg.tab.r.drawable.main_bg);
}else if(event.getactionmasked()== motionevent.action_down) {
this.setbackgroundresource(com.wxg.tab.r.drawable.hui);
}
return super.ontouchevent(event);
}

去除radiobutton前面的圆点adiobutton.setbuttondrawable(android.r.color.transparent);
3.使用xml文件定义,在java代码中使用 radiobutton.setbackgroundresource(r.drawable.radio);调用