Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

Slide Toggle for Android

Anyone know of any open source implementation of a slide toggle for android. The default android toggle(ToggleButton) is not pretty. I am looking for anything similar to iOS. I should be able to implement one from scratch. But if anything similar is already available, then i can build on it.

Thanks in advance to the wonderful stackoverflow community.

Edit1: What I meant by iOS Slide Toggle is UISwitch

ios Toggle Button



Edit2: Just want to summarize the answer. Commonsware provided the clue. I ended up back porting the Switch code from 4.0 to2.2.2. Thanks to the open-sourced code, back porting was not very difficult. The code is hosted on git hub. http://github.com/pellucide/Android-Switch-Demo-pre-4.0/tree/master/

A screenshot from that project
Screen shot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use the sliding drawer widget in android to have a sliding toggle switch. you just have to "slice" the ios toggle images into3 parts, one for the handle, one for the sliding drawer background and one for the content part. then put an image on top of it like a frame to give you the "round edges"

here's what i've come up with: XML Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp" >
        <SlidingDrawer
            android:id="@+id/slidingDrawer1"
            android:layout_width="154dp"
            android:layout_height="54dp"
            android:background="@drawable/ios_retina_toggle_on_full"
            android:content="@+id/content"
            android:handle="@+id/handle"
            android:orientation="horizontal" >
            <ImageButton
                android:id="@+id/handle"
                android:layout_width="54dp"
                android:layout_height="54dp"
                android:background="#00000000"
                android:src="@drawable/ios_retina_toggle_button" />
            <ImageView
                android:id="@+id/content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ios_retina_toggle_off" />
        </SlidingDrawer>
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ios_retina_toggle_frame" />
    </FrameLayout>
</LinearLayout>

ios_retina_toggle_on_full
ios_retina_toggle_on_full.png

ios_retina_toggle_button
ios_retina_toggle_button.png

ios_retina_toggle_off
ios_retina_toggle_off.png

ios_retina_toggle_frame
ios_retina_toggle_frame.png

and finally a screenshot of how it looked on the emulator on a 3.7 WVGA screen running gingerbread: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...