我无法自定义Android Switch小部件的外观.我有自定义xml drawables,我想用于拇指(通常显示为On或Off的小按钮部分)和轨道(拇指滑过的背景).当我使用android:thumb设置拇指时,它工作正常.当我设置轨道时(无论是否设置了拇指),开关完全消失,我只剩下文字显示.
只需应用拇指,这是我的代码:
<com.blahblahblah.blah.CustomSwitch
android:id="@+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off"
android:textOn="On"
android:text="Something Awesome"
android:textColor="@android:color/black"
android:thumb="@drawable/custom_switch_thumb" />
Run Code Online (Sandbox Code Playgroud)
以下是预览窗口中的内容:

并应用跟踪:
<com.blahblahblah.blah.CustomSwitch
android:id="@+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off"
android:textOn="On"
android:text="Something Awesome"
android:textColor="@android:color/black"
android:track="@color/track_color" />
Run Code Online (Sandbox Code Playgroud)
应用了轨道的预览窗口:

作为参考,我在OSX 10.7.5上使用Android Studio 0.2.3.
小智 22
我偶然发现了同样的问题,并在HoloEverywhere问题跟踪器上找到了解决方案.您需要设置<size>正在使用的可绘制XML形状.
不工作(小部件在那里,我可以与它进行交互,但我看不到它,它是隐藏的):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/black_50" />
</shape>
Run Code Online (Sandbox Code Playgroud)
工作(不是补充<size>):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/black_50" />
<size android:width="56dp" android:height="20dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)