我switch在我的应用程序中使用了以下内容。
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text=""
android:thumb="@drawable/toggle_button_color"
android:textOff="@string/text_estimate"
android:textOn="@string/text_accurate"
android:textColor="@color/white" />
在上面,switch我分别在打开和关闭toggle_button_color.xml时将拇指颜色更改为绿色和红色。switch
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@color/red" />
<item android:state_checked="true" android:drawable="@color/green" />
</selector>
如果我将它添加switch到活动布局中,然后它的工作将完美无缺,如下图所示。

但是,如果我switch在Dialog使用时添加它,m_dialog.setContentView(R.layout.mylayout);则开关如下所示。请注意,这mylayout.xml是layout我在其中添加的文件switch。

对于低于 5.0 的 android 版本,棒棒糖switch可以正常工作。请注意,由于某些原因,我Theme.Holo.Light在我的应用程序中使用,所以我不能使用SwitchCompat.
我知道在点击 Android 5.0 时 Switch crashs已在此处提出了类似的问题。
而且这里也有报道https://code.google.com/p/android-developer-preview/issues/detail?id=1704。我也尝试了上面链接中提到的为拇指和轨道添加可绘制图像的工作,但我不明白为什么同一个开关正在工作activity layout但不在Dialog.
有人可以帮我解决这个问题吗?