Android第三方开源SwitchButton

来源:互联网 发布:matlab元胞转化为矩阵 编辑:程序博客网 时间:2024/06/11 10:28


Android第三方开源SwitchButton

Android SwitchButton是github上的一个第三方开源项目,其项目主页是:https://github.com/kyleduo/SwitchButton
Android平台上的Switch Button样式单一,SwitchButton旨在丰富Android平台的Switch样式的Button,其实现的结果如图:



注意到SwitchButton其中一个实现,就是iOS样式的Switch切换开关。
SwitchButton本身给出实例代码结构不是很简单直观,我把其中关于iOS的SwitchButton实现,抽取出来单独写一个例子结果如图:


布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="zhangphil.demo.MainActivity">    <com.kyleduo.switchbutton.SwitchButton        android:id="@+id/switchButton"        style="@style/SwitchButtonStyle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:checked="false"        app:kswAnimationDuration="300"        app:kswBackDrawable="@drawable/ios_back_drawable"        app:kswBackMeasureRatio="1.4"        app:kswThumbDrawable="@drawable/ios_thumb_selector"        app:kswThumbMarginBottom="-8dp"        app:kswThumbMarginLeft="-5dp"        app:kswThumbMarginRight="-5dp"        app:kswThumbMarginTop="-2.5dp" /></RelativeLayout>


上层Java代码:

package zhangphil.demo;import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.Toast;import com.kyleduo.switchbutton.SwitchButton;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        SwitchButton mSwitchButton = (SwitchButton) findViewById(R.id.switchButton);        mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                String s;                if (b)                    s = "选中";                else                    s = "未选";                Toast.makeText(getApplication(), s, Toast.LENGTH_SHORT).show();            }        });    }}



我写的这个例子已经push到github上,该代码的项目主页是:https://github.com/zhangphil/Android_iOS_SwitchButton


附录文章:
1,《Android Segmented RadioButton》链接地址:http://blog.csdn.net/zhangphil/article/details/51441677 
2,《Android选项切换条SHSegmentControl》链接地址:http://blog.csdn.net/zhangphil/article/details/49720805
3,《Android ToggleButton:状态切换的Button》链接地址:http://blog.csdn.net/zhangphil/article/details/51720565

0 0
原创粉丝点击