How to have a Lollipop switch button

自作多情 提交于 2019-12-03 09:28:58

问题


I want to have the Lollipop style switch button for my app:

How could I implement this button so it looks like this also on older versions of android?


回答1:


At first set android:targetSdkVersion="22" in your manifest to make your app compatible to Lollipop.

NOTE: Color of your switch depends on this

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

Create your own theme for your app in styles.xml in Folder values-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 in default Folder values or values-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>



回答2:


To have the Lollipop style switch button on older versions of android you should use SwitchCompat in layout xml file

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

and also in java file

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



回答3:


There is a great article on the Android Developers Blog that discusses how to use material design on pre-Lollipop devices: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

To more specifically answer your question, you can use the Lollipop style switch for older versions by using the SwitchCompat API: https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html




回答4:


API 24 on off switch

<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"/>



回答5:


I think what you need is in that library

what this library does is to allow you to create material design switch button like in andorid 5.0

https://github.com/kyleduo/SwitchButton




回答6:


We are using SwitchCompact in lollipop version or either you can use Updated lolliopop version its better

<android.support.v7.widget.SwitchCompat
    android:id="@+id/compatSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/switch2"
    android:layout_alignLeft="@+id/switch2"
    android:layout_alignStart="@+id/switch2"
    android:layout_marginTop="39dp" />



回答7:


In order to resolve old type switch

switch_old

  1. update your minsdkversion and targetsdkversion to 19 and 28 respectively.
    1. update gradle dependency to implementation 'com.android.support:appcompat-v7:28.0.0'
    2. set base app theme in style.xml like

`

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    </style>`

and it resolve switch with latest material design.



来源:https://stackoverflow.com/questions/29752235/how-to-have-a-lollipop-switch-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!