2016-01-13 103 views
8

我創建了一個基本的自定義Switch,如下所述。自定義Android切換軌道'動畫'

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

此開關看起來和預期大部分工作,但有一些方法來「動畫」軌道爲拇指行駛在其上的用戶拖動?或者在選中或取消選中之間淡入淡出,或者最好在拇指後面改變。

當前行爲如下所示。

Current behaviour

+1

更好地利用搜索條,使其custom..either使用的toogle –

+0

http://stackoverflow.com/questions/9752760/slide-切換爲Android –

+0

https://github.com/tuesda/SwitchBox –

回答

5

曾經有一段時間我也一直在尋找同樣的,當我需要類似的可以拖動的開/關我的項目之一,提供原生iOS切換按鈕的功能。那時我努力搜索,我找到了這個圖書館。

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

所以希望這是你在找什麼了。

+0

這看起來像我以後的事情,驚訝有多難搜索任何與交換機有關的任何事情... 謝謝,我會檢查出來! – BlitzKraig

+0

與圖書館一起玩,並設法取得體面的效果。這有點煩瑣,特別是在使用更新的API版本時,必須添加一些hacky代碼才能讓它保持良好。雖然這項工作很好,但非常感謝您的幫助。 – BlitzKraig

+0

是的,它有點棘手定製,但很高興它爲你工作:) –

1

一個開關是一個雙狀態切換開關小部件,可以選擇 兩個選項。用戶可以前後拖動「拇指」來選擇 所選的選項,或者只需輕點即可切換,就好像它是一個複選框。 text屬性控制 開關標籤中顯示的文本,而off和on文本控制拇指上的文本。

對於這個要求,你需要定製您的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 .Thumb將繪製實際外觀或更改狀態。

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> 

this