【问题标题】:How to have a Lollipop switch button如何有一个棒棒糖开关按钮
【发布时间】:2019-07-31 17:09:08
【问题描述】:

我希望我的应用拥有棒棒糖风格的切换按钮:

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

【问题讨论】:

    标签: android android-5.0-lollipop android-switch


    【解决方案1】:

    要在旧版本的 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);
    

    【讨论】:

    • 此外,您可以使用以下代码去除选择开关时出现的难看的灰色背景:android:background="@null"
    【解决方案2】:

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

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

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

    为您的应用创建自己的主题 styles.xml 在文件夹 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 在默认文件夹 valuesvalues-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>
    

    【讨论】:

    • 不要设置maxSdkVersion。这会将您的安装限制为 lolipop。你需要的是targetSdkVersion
    • @MirceaNistor 你是对的。我更新了我的答案。谢谢:)
    【解决方案3】:

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

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

    【讨论】:

    【解决方案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"/>
    

    【讨论】:

      【解决方案5】:

      我认为你需要的是那个库

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

      https://github.com/kyleduo/SwitchButton

      【讨论】:

      • 请添加更多关于库的信息,而不仅仅是发布链接。
      • 感谢您发布此链接。我不知道这个图书馆。并单击链接提供了更多信息!去图吧。
      • @MartyMiller 在右下角有一个下载按钮,您可以从那里下载该库
      【解决方案6】:

      我们在棒棒糖版本中使用 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" />
      

      【讨论】:

      【解决方案7】:

      为了解决老类型的开关

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

      它使用最新的材料设计解决了开关问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        • 2015-08-12
        • 2014-12-15
        • 1970-01-01
        • 2015-09-24
        相关资源
        最近更新 更多