Android:使用文本和自定义视图切换

Android:使用文本和自定义视图切换,android,canvas,view,switch-statement,Android,Canvas,View,Switch Statement,我需要开发一个按钮,如下图所示: 如何自定义交换机?或者,如果不可能,如何实现此自定义视图?试试这个 <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toogle_switch" android:text="" a

我需要开发一个按钮,如下图所示:

如何自定义交换机?或者,如果不可能,如何实现此自定义视图?

试试这个

 <Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toogle_switch"
    android:text=""
    android:textOn=""
    android:textOff="" />

toogle_switch.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/on" android:state_checked="true"></item>
   <item android:drawable="@drawable/off" android:state_checked="false"></item>
</selector>

试试这个

 <Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toogle_switch"
    android:text=""
    android:textOn=""
    android:textOff="" />

toogle_switch.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/on" android:state_checked="true"></item>
   <item android:drawable="@drawable/off" android:state_checked="false"></item>
</selector>


RelativeLayout布局=(RelativeLayout)findViewById(R.id.R_布局);
开关sb=新开关(本);
某人(指“关”);
b.塞特克斯顿(“ON”);
某人被检查(正确);
布局。添加视图(sb);
开关sw=(开关)findViewById(R.id.switch1);
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
//切换已启用
}否则{
//切换被禁用
}
}
});

RelativeLayout布局=(RelativeLayout)findViewById(R.id.R_布局);
开关sb=新开关(本);
某人(指“关”);
b.塞特克斯顿(“ON”);
某人被检查(正确);
布局。添加视图(sb);
开关sw=(开关)findViewById(R.id.switch1);
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
//切换已启用
}否则{
//切换被禁用
}
}
});