Android开发:仿苹果iPhone 切换按钮SwitchButton的实现

目录

引言 

效果图

一、在您的应用程序中使用SwitchButton

 二、布局中使用

 三、SwitchButton的属性值

 四、需要注意的地方

结束语


 

 

引言 

项目开发中用到了switch切换按钮,所以在网上找了一个用的比较多的控件,这里记录一下用法。

效果图

 

一、在您的应用程序中使用SwitchButton

该控件github地址:https://github.com/kyleduo/SwitchButton

在Gradle 中,在模块的build.gradle中添加依赖项 

dependencies {
	implementation 'com.kyleduo.switchbutton:library:2.0.0'
}

 二、布局中使用

  <com.kyleduo.switchbutton.SwitchButton
                        android:id="@+id/switchKongZhiPeiJian"
                        android:layout_width="@dimen/view_width_50"
                        android:layout_height="28dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        app:kswBackDrawable="@drawable/switchbutton_selector"
                        app:kswThumbColor="@color/white"
                        app:kswThumbHeight="28dp"
                        app:kswThumbMargin="@dimen/margin_1"
                        app:kswThumbRadius="@dimen/margin_14"
                        app:kswThumbWidth="28dp"
                        app:kswTintColor="@color/green" />

 

 三、SwitchButton的属性值

nameinfo
kswThumbDrawable手柄
kswThumbColor手柄颜色
kswThumbMargin手柄周边margin
kswThumbMarginTop上外边距
kswThumbMarginBottom底外边距
kswThumbMarginLeft左外边距
kswThumbMarginRight右外边距
kswThumbWidth手柄宽度(与高度等时是个圆)
kswThumbHeight手柄高度
kswThumbRadius手柄的圆角 需要:kswThumbColor
kswBackRadius除手柄外的背景色需要:kswBackColor
kswBackDrawable背景图
kswBackColor背景字体颜色
kswFadeBack启用动画?
kswAnimationDuration动画持续时间
kswTintColorchange SwitchButton’s style just by one property, all relevant color will be generate automatically. Do not support SwitchButtonMD or other style created by xml resources.
kswTextOn开文字
kswTextOff关文字
kswTextThumbInset (since 2.0.0)length of the part of text under the thumb.
kswTextExtra (since 2.0.0)extra space needed by background besides the actual text width.
kswTextAdjust (since 2.0.0)文字自适应
kswThumbRangeRatio (since 2.0.0)手柄移动范围长条滑动
kswBackMeasureRatio(since 2.0.0)移除
kswTextMarginH(since 2.0.0)移除
kswAutoAdjustTextPosition(since 2.0.0)移除

 

 四、需要注意的地方

在我的项目中,需要实现选中和非选中状态的背景图不同。试了全部的属性值,似乎都不能解决,在绝望之际,突然迸发出一丝灵感。那就是使用kswBackDrawable属性为SwitchButton设置背景图,自定义selector,选中和未选中状态显示不同的颜色。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/selector_switchbutton_selected" android:state_selected="true" />
    <item android:drawable="@drawable/selector_switchbutton_selected" android:state_checked="true" />
    <item android:drawable="@drawable/selector_switchbutton_no_selected" />
</selector>

除此之外,switchbutton存在两种选中方式,分别为:

switchKongZhiPeiJian.setCheckedNoEvent(false);
switchKongZhiPeiJian.setChecked(false);

 

setCheckedNoEvent():只是走实际的选中事件,只是实现了选中的效果
setChecked():会经过选中事件,

 

结束语

如有问题欢迎交流

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值