[英]android-How to make custom toggle button
我想創建一個自定義切換按鈕,例如,當我單擊該按鈕時,星星會打開,而當我再次單擊時,星星會關閉。 我嘗試了一些代碼,但是它們將按鈕更改為圖像,當我單擊它時,圖像發生了變化。 我的意思是,沒有按鈕,只是一個圖像。
我希望圖像位於按鈕內部,就像默認的切換按鈕一樣,我只需要更改其中的圖像即可。
我該怎么辦?
謝謝
在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>
將選擇器應用於切換按鈕
<ToggleButton
android:id="@+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
注意:刪除上面代碼中我使用的文本
textOff=""
textOn=""
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.