1

我需要创建如下图所示的开关 必需的

我有它的开和关图像。我将它们实现为

   <android.support.v7.widget.SwitchCompat
                    android:id="@+id/swtichTournament"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:showText="false"
                    android:thumb="@drawable/switch_selector"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:checked="false" />

选择器 XML

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_off" />
<item android:state_pressed="true"  android:drawable="@drawable/switch_on" />
<item android:state_checked="true"  android:drawable="@drawable/switch_on" />
<item                               android:drawable="@drawable/switch_off" />

这就是我得到的

我的输出

如您所见,有一些额外的颜色不是必需的。我该如何删除它?

4

3 回答 3

4

如果您使用的是 AppCompat 库,请将您的 switch 元素替换为 SwitchCompat 元素,您将得到您想要的。有关 switchCompat 的更多信息,请参阅:https ://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html

于 2015-04-30T08:16:30.160 回答
2

您可以在 XML 中将 Switch 的背景设置为透明:

android:background="@android:color/transparent"
于 2015-04-30T08:12:11.703 回答
0

我希望这个链接能帮助你 https://androidician.wordpress.com/2014/09/24/android-custom-toggle-button-example-ios-like-toggle-buttons/

于 2015-04-30T08:53:13.677 回答