【问题标题】:Custom Android switch track 'animation'自定义 Android 切换轨道“动画”
【发布时间】:2016-01-13 11:35:56
【问题描述】:

我创建了一个基本的自定义Switch,定义如下。

<Switch
        android:id="@+id/availSwitch"
        android:layout_width="wrap_content"
        android:switchMinWidth="110dp"
        android:layout_height="wrap_content"
        android:track="@drawable/switch_track"
        android:thumb="@drawable/thumb"/>

@drawable/thumb 是一个简单的 PNG,可以正常工作。

@drawable/switch_track 定义如下。 @drawable/trackon@drawable/trackoff 是 PNG 的。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/trackoff" />
    <item android:state_checked="true"  android:drawable="@drawable/trackon" />
    <item                               android:drawable="@drawable/trackoff" />

</selector>

这个开关在大多数情况下看起来和工作正常,但是当拇指在用户拖动时在轨道上移动时,是否有某种方法可以“动画化”轨道?在选中和未选中之间淡入淡出,或者最好在拇指后面改变。

当前行为如下所示。

【问题讨论】:

标签: android android-layout android-drawable android-switch


【解决方案1】:

有一段时间,当我需要一个类似于本机 iOS 的切换按钮功能时,我也在寻找相同的功能,该功能可以为我的一个项目拖到开/关。当时我苦苦搜索,找到了这个库。

https://github.com/pellucide/Android-Switch-Demo-pre-4.0

所以希望这也是您正在寻找的。​​p>

【讨论】:

  • 这看起来像我所追求的那种东西,很惊讶搜索任何与 Android 的 Switch 相关的东西是多么困难......谢谢,我会检查一下!
  • 玩弄了图书馆并设法获得了不错的效果。这有点繁琐,尤其是在使用较新的 API 版本时,必须添加一些 hacky 代码才能让它坐好。不过这项工作做得很好,非常感谢您的帮助。
  • 是的,定制起来有点棘手,但很高兴它对你有用:)
【解决方案2】:

Switch 是一种具有两种状态的切换开关小部件,可以在 两个选项。用户可以来回拖动“拇指”来选择 选择的选项,或者只需点击以切换,就好像它是一个复选框一样。 text 属性控制在标签中显示的文本 开关,而关闭和打开文本控制拇指上的文本。

为此,您需要自定义您的Switch 按钮功能

您可以访问演示

  1. Slide Toggle for Android

  2. Android-Switch-Demo-pre-4.0

  3. Custom Toggle Button for Android Applications

你需要了解以下两行。

        android:thumb="@drawable/customswitchselector"
        android:track="@drawable/custom_track"

它有两个标签:android:thumb and android:track。当我们滑动或更改状态时,拇指将绘制实际外观。

【讨论】:

    【解决方案3】:

    您需要自定义切换按钮

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/darkGray"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.gisinc.androidexamples.myapplication.MyActivity">
    
        <com.gisinc.androidexamples.androidtogglebutton.SettingsToggle
            xmlns:widget="http://schemas.android.com/apk/res-auto"
            android:id="@+id/settings1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            widget:prefName="useMyLocation"
            widget:text="Use My Location" />
    
    </RelativeLayout>
    

    this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多