2013-10-30 33 views
1

我跟着答案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> 
+0

您正在使用它错误,或者至少它太复杂。试图在此刻进行分类。 – bofredo

+0

想我做到了错误的style.xml而我编辑过,但仍然有同样的问题 –

+0

我总是用的,而不是style.xml styles.xml。虽然我不知道这是否会带来麻烦。 – bofredo

回答

1

按照三步后,所有的应用程序的togglebutton得到了新的风格!

toggle.xml:(绘制文件夹)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="false" android:drawable="@drawable/toggle_off" /> 
    <item android:state_checked="true" android:drawable="@drawable/toggle_on" /> 
</selector> 

styles.xml:(值文件夹)

<style name="toggle" parent="@android:style/Widget.Button.Toggle"> 
    <item name="android:background">@drawable/toggle</item> 
    <item name="android:textOn"> </item> 
    <item name="android:textOff"> </item> 
</style> 

和最后一步你要教,你有一个当前AppTheme自定义切换按钮:

<style name="AppTheme" parent="android:Theme.Holo.Light"> 
    <item name="android:buttonStyleToggle">@style/toggle</item> 
</style> 
+0

是的,我认为我做了这3步正确的问题是如何编码,所以它可以从关闭打开,当我点击它。就像你我的主要活动写什么,希望你能帮助欢呼 –

+0

你只想使用标准的拨动按钮变“开/关”到“开/打开”!?!现在我很困惑:)# – bofredo

+0

我想实现一个开/关自定义设计。现在的问题是,当我运行应用程序,并单击切换按钮,它不会从关闭到打开。所以我想知道我在哪里做错了,是因为我没有编码或?对不起,如果我混淆你(我英文很差) –