Android中如何选择SwitchButton

1,系统自带switch

缺点:不能自定义设置宽高,不建议使用

2,第三方SwitchButton

1com.suke.widget.SwitchButton,github star2k多

<com.suke.widget.SwitchButton
    android:id="@+id/switch_go_home"
    android:layout_width="@dimen/m_iot_dp_100"
    android:layout_height="@dimen/m_iot_dp_54"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="@dimen/m_iot_dp_20"
    android:layout_marginBottom="@dimen/m_iot_dp_10"
    app:sb_background="@color/m_iot_switch_bg"
    app:sb_border_width="@dimen/m_iot_dp_4"
    app:sb_checked_color="#3FA4D6"
    app:sb_show_indicator="false"
    app:sb_uncheck_color="@color/m_iot_switch_bg" />

优点:可以在xml自定义设置宽高

缺点:setonclicklistener没有实现,致命缺点view.isPress()方法无效,无法判断人为切换还是代码切换

2com.kyleduo.switchbutton.SwitchButton

<com.kyleduo.switchbutton.SwitchButton
    android:id="@+id/switch_go_home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="@dimen/m_iot_dp_20"
    app:kswBackDrawable="@drawable/track_selector"
    app:kswThumbRangeRatio="2.5"
    android:layout_marginRight="@dimen/m_iot_dp_20"
    android:layout_marginBottom="@dimen/m_iot_dp_16"
    app:kswThumbDrawable="@drawable/thumb_selector"
    app:kswThumbHeight="@dimen/m_iot_dp_25"
    app:kswThumbMargin="@dimen/m_iot_dp_5"
    app:kswThumbWidth="@dimen/m_iot_dp_25">
</com.kyleduo.switchbutton.SwitchButton>

优点:可以区分人为切换还是代码切换

需要注意的几个属性app:kswBackDrawable底部颜色的selector,app:kswThumbDrawable滑块的背景色的selector,backDrawable的shape不能设置宽高,否则会变形

要想设置它的宽高,必须设置app:kswThumbHeight,app:kswThumbWidth,app:kswThumbRangeRatio,否则无论怎么设置都不会生效,控件的宽=kswThumbWidth*kswThumbRangeRatio+左右的margin

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值