2015-04-20 25 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