22

我想为我的应用程序设置棒棒糖风格的切换按钮:

在此处输入图像描述

我怎样才能实现这个按钮,使它在旧版本的 android 上也看起来像这样?

4

7 回答 7

46

要在旧版本的 android 上使用 Lollipop 样式的切换按钮,您应该在布局 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);
于 2015-08-26T11:48:39.797 回答
11

首先android:targetSdkVersion="22"在您的清单中设置以使您的应用程序与Lollipop兼容。

注意:开关的颜色取决于此

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

在文件夹values-v21中的styles.xml中为您的应用创建自己的主题

<?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>

默认文件夹values-v14中的styles.xml

<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>
于 2015-04-21T15:27:05.397 回答
10

Android 开发者博客上有一篇很棒的文章讨论了如何在棒棒糖之前的设备上使用材料设计:http ://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre .html

要更具体地回答您的问题,您可以使用SwitchCompatAPI 将 Lollipop 样式开关用于旧版本:https ://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html

于 2015-04-20T15:42:17.007 回答
5

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"/>
于 2016-07-18T04:47:44.430 回答
0

我想你需要的是那个图书馆

这个库的作用是让你像在 andorid 5.0 中一样创建材料设计切换按钮

https://github.com/kyleduo/SwitchButton

于 2015-04-20T15:40:52.100 回答
0

我们在棒棒糖版本中使用 SwitchCompact,或者您可以使用更新的棒棒糖版本更好

<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" />
于 2017-01-06T05:29:39.250 回答
0

为了解决旧式开关

switch_old

  1. 将您的 minsdkversion 和 targetsdkversion 分别更新为 19 和 28。
    1. 将 gradle 依赖项更新为实现 'com.android.support:appcompat-v7:28.0.0'
    2. 在 style.xml 中设置基本应用主题,例如

`

<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>`

并采用最新材料设计解决开关。

于 2019-01-23T05:23:11.560 回答