我跟着答案Android: Create a toggle button with image and no text一切都很好,但是当我载入我的应用程序,当我点击我的切换按钮,没有发生像它并没有从关闭状态改变为开启状态。那么,我需要做些什么才能使滑动工作?如果需要,我是否需要编码?我应该编码什么? 感谢 btn_toggle_bg.xml创建拨动/滑动按钮
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@android:color/transparent" />
<item android:id="@+android:id/toggle"
android:drawable="@drawable/btn_toggle" />
</layer-list>
btn_toggle.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/close" />
<item android:state_checked="true"
android:drawable="@drawable/open" />
</selector>
layout.xml
<ToggleButton
android:id="@+id/toggleButton"
style="@style/toggleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/btn_toggle_bg"
android:onClick="onToggleClicked"
android:layout_marginLeft="150dp" />
style.xml
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<style name="toggleButton" parent="@android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
</style>
您正在使用它错误,或者至少它太复杂。试图在此刻进行分类。 – bofredo
想我做到了错误的style.xml而我编辑过,但仍然有同样的问题 –
我总是用的,而不是style.xml styles.xml。虽然我不知道这是否会带来麻烦。 – bofredo