我在HorizontalScrollView中有几个ToggleButtons,我希望每个ToggleButtons都有不同的on-off drawable
切换按钮:
<ToggleButton
android:id="@+id/toggle_[nameOfToggle]"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/toggle_selector"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
我有一个选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- currently pressed turning the toggle on -->
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/toggle_state_on"/>
<!-- currently pressed turning the toggle off-->
<item android:state_pressed="true"
android:drawable="@drawable/toggle_state_off"/>
<!-- not pressed default checked state -->
<item android:state_checked="true" />
<!-- Default non-pressed non-checked -->
<item />
</selector>
toggle_state_on.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_on_[nameOfToggle]"/>
</layer-list>
以编程方式实现此目的的最佳方法是,有没有办法检查它是什么切换按钮并根据它更改开/关状态的可绘制内容?