2012-02-17 70 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>