8

我创建了一个基本的 custom 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>

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

当前行为如下所示。

当前行为

4

3 回答 3

5

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

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

所以希望这也是你正在寻找的。

于 2016-01-13T11:55:14.950 回答
1

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

对于此要求,您需要自定义Switch按钮功能

您可以访问演示

  1. 适用于 Android 的滑动切换

  2. Android-Switch-Demo-pre-4.0

  3. Android 应用程序的自定义切换按钮

您需要了解以下两行。

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

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

于 2016-01-13T12:03:40.353 回答
0

您需要自定义切换按钮

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

看到这个

于 2016-01-13T12:07:30.340 回答