2012-02-17 72 views
19

ICS有一个开关组件。它做我们需要的。是否有任何向后兼容(约)2.2?找不到任何明显的东西。向后兼容开关


貌似有人建立了这个:

https://github.com/Prototik/KFramework-SW.git

+5

链接不起作用了 – mprabhat 2013-03-25 09:12:04

+0

怎么样的:https://github.com/yongjhih/SwitchPreferenceCompat https://github.com/ankri/SwitchCompatLibrary https://开头github上。com/BoD/android-switch-backport – 2015-05-13 20:36:44

回答

10

交换机仅在4.0+

如果你想使用开关4.0+设备,你需要什么样的应用do是声明两个布局。 layout-v14中的第一个将在ICS设备上使用。在您的布局文件夹中使用CheckBox。

在您的代码中,在从交换机或复选框获取/设置数据时,请使用CompoundButton类。你会发现CompoundButton对此很有效。

+0

我不明白你的答案。我了解文件夹命名约定。我不明白你用CompoundButton得到什么。总之,答案是“不”,对吗? – 2012-05-11 23:31:06

+1

我试图说你可以使用ICS上的开关,但在Pre ICS上使用复选框。但是,是的,总之答案是“否”。 – Lee 2012-05-12 09:09:32

+0

只是为了确认,截至今天,没有开源兼容性资源。如果我耽误时间,可能会创建一个。真的很喜欢它的工作方式。 – 2012-09-06 03:26:18

4

这个库是你在找什么:https://github.com/BoD/android-switch-backport

+0

另一个:http://www.androidviews.net/2012/12/switch-compat/ – 2013-03-25 14:40:45

+0

我试过第一个,它效果很好。代码的质量可能会更好(几乎没有javadoc,有时很难理解,难以修改)。但是这个库完全做得很好,并且提供了可以完全主题化的开关。我们通过[Android Holo Colors](http://android-holo-colors.com/)获得了很好的结果。 – Snicolas 2013-03-25 17:08:29

+0

我尝试了两种方法,他们工作得很好,除了在某些设备上出现严重问题:第一次正确主题的活动包含开关时,应用程序崩溃,因为未找到文本属性之一,导致文本内容为空指针!就好像主题没有设置一样,有没有人遇到过这个问题并有解决方案? – 3c71 2013-05-10 14:22:16

0

如果你正在使用holoeverywhere库,你可以在你的布局文件中使用类似这样的东西:

<org.holoeverywhere.widget.Switch 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
/> 
44

Android支持来自版本21.0.0的AppCompat库包含android.support.v7.widget.SwitchCompat以提供可恢复到API v7的兼容性。 https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html

包括像这样与gradle这个:

compile 'com.android.support:appcompat-v7:21.0.0' 

它可以在布局中使用这样的:

<android.support.v7.widget.SwitchCompat /> 

此外,它还具有showText属性,使造型更加容易 - 这似乎是从本地andriod Switch丢失。

+0

它适用于我,SwitchCompat在4.x sytle(不显得丑陋)开关显示切换像5.x 6.x(看起来很好),它很好改变切换到SwitchCompat! – shuabing 2017-03-23 08:53:42

4

这里是SwitchCompat

第一件事为例做确保您此行添加到您的的build.gradle,然后同步。

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.0.0' 
} 

其次创建示例活动,在我来说,我将其称之为SwitchActivity.java

public class SwitchActivity extends ActionBarActivity { 

     SwitchCompat mySwitch = null; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_switch); 

      // here is your switch 
      mySwitch = (SwitchCompat)findViewById(R.id.myswitch); 

     } 
     ..... 
} 

最后,创建您的布局,在我来说,我将其称之为activity_switch.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.neoecosystem.samplex.SwitchActivity"> 

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/myswitch" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

</RelativeLayout>