2015-04-20 26 views

回答

9

起初設定android:targetSdkVersion="22"在你的清單,讓您的應用棒棒糖兼容。

注:交換機的顏色取決於這個

<item name="android:colorAccent">@color/accent</item> 

styles.xml在文件夾中創建自己的應用程式的主題值-V21

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <item name="android:colorPrimary">@color/primary</item> 
     <item name="android:colorPrimaryDark">@color/primary_dark</item> 
     <item name="android:colorAccent">@color/accent</item> 
     <item name="android:textColorPrimary">@color/text_primary</item> 
     <item name="android:textColor">@color/text_secondary</item> 
     <item name="android:navigationBarColor">@color/primary_dark</item> 
     <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item> 
    </style> 
</resources> 

styles.xml默認文件夾值-V14

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <!-- Customize your theme here. --> 

     <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item> 
    </style> 

    <style name="AppTheme.Base" parent="Theme.AppCompat"> 
     <!-- Customize your theme here. --> 

     <!-- colorPrimary is used for the default action bar background --> 
     <item name="colorPrimary">@color/primary</item> 

     <!-- colorPrimaryDark is used for the status bar --> 
     <item name="colorPrimaryDark">@color/primary_dark</item> 

     <!-- colorAccent is used as the default value for colorControlActivated 
      which is used to tint widgets --> 
     <item name="colorAccent">@color/accent</item> 

     <!-- You can also set colorControlNormal, colorControlActivated 
      colorControlHighlight & colorSwitchThumbNormal. --> 
    </style> 

</resources> 
+1

不要設置'maxSdkVersion'。這將限制你的安裝到lolipop。你需要的是'targetSdkVersion' –

+0

@MirceaNistor你是對的。我更新了我的答案。謝謝:) – ch3tanz

0

我想你需要的是在該庫

這是什麼庫做是爲了讓你像的Andorid 5.0

https://github.com/kyleduo/SwitchButton

+1

其更好
'輸入代碼,請添加更多的信息是關於libary,而不是僅僅發佈一個鏈接。 – JacksOnF1re

+0

感謝您發佈此鏈接。我不知道這個圖書館。點擊鏈接提供更多信息!去搞清楚。 –

+0

@MartyMiller在右下角有一個下載按鈕,從那裏你可以下載該庫 –

10

有一個偉大的文章上創建材料設計開關按鈕討論如何在預棒棒糖設備上使用材料設計的Android開發人員博客:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

要更具體地回答您的問題,可以使用棒棒糖風格的開關CH舊版本使用SwitchCompat API:https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html

+2

很好的答案!對於SwitchCompat造型,請看這裏: http://stackoverflow.com/a/26715076/457059 – stoefln

+0

這應該是真正的問題... – desgraci

35

要對舊版本的Android的棒棒糖風格切換按鈕,你應該在佈局XML文件中使用SwitchCompat

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

,並在Java文件

SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.compatSwitch); 
+2

此外,您可以刪除使用這段代碼選擇開關時出現的醜陋灰色背景:android:background =「@ null」 – Tobliug

4

API 24關閉開關

  <android.support.v7.widget.SwitchCompat 
      android:id="@+id/switch1" 
      android:layout_alignBottom="@+id/textView3" 
      android:layout_alignEnd="@+id/input_layout_password" 
      android:layout_alignRight="@+id/input_layout_password" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
0

我們正在棒棒糖版本使用SwitchCompact或要麼你可以使用更新版本lolliopop這裏

+0

+0

您可以編輯您的答案 – Saveen