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不能设置宽高,否则会变形