我想在 Android 5.0 和 android 5.0 应用程序上为 Switch 创建选择器

I wanna create selector for Switch on both Android 5.0 and less android 5.0 application

我想为 Android 5.0 和更低的 android 5.0 应用程序创建主题。 例如,在第一种情况下,我有一个开关,如果我不设置任何背景,它的外观将在 Android 5.0 上更改为如您所知的滑动按钮,

但在 less android 5.0 中它显示为 on/off 按钮。

当我把

android:thumb="@drawable/switch_selector"

要更改缩略图图像和颜色,在 android 4.4 上没问题,但我的 Switch 在 Android 5.0 上什么都不显示。我的问题是如何为 Switch 创建一个可以在所有设备上显示的选择器或主题?

已更新

以简单的方式,我们为每个屏幕 dpi 设置了许多可绘制文件夹。我想知道我是否可以像每个 dpi 一样为每个 Api 设置可绘制对象?!

在经历了 Android 通知的艰难时期之后,我需要为 android 具有 Android 5.x 及更高版本的设备执行一种方法,另一种方法用于 pre-Lollipop 设备,我建议您搜索像这样的其他库

SwitchButton

This project provides you a convenient way to use and customise a SwitchButton widget in Android. With just resources changed and attrs set, you can create a lifelike SwitchButton of Android 5.0+, iOS, MIUI, or Flyme and so on.

Now we get the biggest movement since SwitchButton published. v1.3.0 comes with totally reconsitution and more convenient API. A wholly new demo can give you a tour in it.

Github page: https://github.com/kyleduo/SwitchButton More like this you would find here: http://android-arsenal.com/

其他库可能会帮助您避免 Android 组件的向后兼容性问题。

如你所说,你添加这行代码:

   android:thumb="@drawable/switch_selector"

它适用于 Kitkat,但不适用于 Lollipop。

我已经发现在 Lollipop 上您可能有更多工作要做。 看看这里:

所以试着像上面这样找到你想要的额外库,让我知道它是如何工作的;-)

编辑: 要获得 API 版本,请使用:

Build.VERSION.RELEASE;

That will give you the actual numbers of your version; aka 2.3.3 or 2.2. The problem with using Build.VERSION.SDK_INT is if you have a rooted phone or custom rom, you could have a none standard OS (aka my android is running 2.3.5) and that will return a null when using Build.VERSION.SDK_INT so Build.VERSION.RELEASE will work no matter what!

To use it, you could just do this;

String androidOS = Build.VERSION.RELEASE;

From: Retrieving Android API version programmatically