[英]android-How to make custom toggle button
I want to make a custom toggle button , for instance when I click on the button , the star turns on and when I click again ,the star turns off. 我想创建一个自定义切换按钮,例如,当我单击该按钮时,星星会打开,而当我再次单击时,星星会关闭。 i've tried some codes but they change the button to an image and when I click on it ,the image changes .
我尝试了一些代码,但是它们将按钮更改为图像,当我单击它时,图像发生了变化。 I mean ,there is no button , it's just an image .
我的意思是,没有按钮,只是一个图像。
I want the image to be inside the button , like the default toggle button and I just need to change the image inside it . 我希望图像位于按钮内部,就像默认的切换按钮一样,我只需要更改其中的图像即可。
How can I do so ? 我该怎么办?
thanks 谢谢
create toggle_selector.xml in res/drawable 在res / drawable中创建toggle_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button 将选择器应用于切换按钮
<ToggleButton
android:id="@+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code 注意:删除上面代码中我使用的文本
textOff=""
textOn=""
reference https://stackoverflow.com/a/20300753/712960 参考https://stackoverflow.com/a/20300753/712960
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.