该控件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" />
name | info |
---|
kswThumbDrawable | 手柄 |
kswThumbColor | 手柄颜色 |
kswThumbMargin | 手柄周边margin |
kswThumbMarginTop | 上外边距 |
kswThumbMarginBottom | 底外边距 |
kswThumbMarginLeft | 左外边距 |
kswThumbMarginRight | 右外边距 |
kswThumbWidth | 手柄宽度(与高度等时是个圆) |
kswThumbHeight | 手柄高度 |
kswThumbRadius | 手柄的圆角 需要:kswThumbColor |
kswBackRadius | 除手柄外的背景色需要:kswBackColor |
kswBackDrawable | 背景图 |
kswBackColor | 背景字体颜色 |
kswFadeBack | 启用动画? |
kswAnimationDuration | 动画持续时间 |
kswTintColor | change 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():会经过选中事件,
如有问题欢迎交流
目录引言效果图一、在您的应用程序中使用SwitchButton二、布局中使用三、SwitchButton的属性值四、需要注意的地方结束语引言项目开发中用到了switch切换按钮,所以在网上找了一个用的比较多的控件,这里记录一下用法。效果图一、在您的应用程序中使用SwitchButton该控件github地址:https...
编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/60480014
Demo展示图片添加依赖 在project的build.gradle—>buildscript—>repositories节点下添加:mavenCentral() 如图:
在module的dependencies节点下添加:com
SwitchButton。一个美观,轻巧,易于定制的Android开关小部件,minSdkVersion> = 11 欢迎发表文章〜
-取决于没有第三方库-没有原始文件(图片/可绘制图形等),只有一个Java和style.xml文件-拖动开关支持
Gradle:
repositories {
mavenCentral()
jcenter()
dependencies {
compile 'com.github.zcweng:switch-button:0.0.3@aar'
layout.xml:
< LinearLayout xmlns : android = " http://schemas.android.com/apk/res/android "
android : layout_wid
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xml...
Android第三方开源SwitchButton
Android SwitchButton是github上的一个第三方开源项目,其项目主页是:https://github.com/kyleduo/SwitchButton
Android平台上的Switch Button样式单一,SwitchButton旨在丰富Android平台的Switch样式的Button,其实现的结果如...
* SwitchButton开关控件早已经非常流行。有各种各样的样式,SwitchButton开关控件一般用于app软件设置那里,控制缓存、声音、提示、下载等等。是具有很好的UI体验以及用户的习惯性
Part 1、使用自定义View来实现SwitchButton
今天介绍的开源项目是否的优秀,又是国人的作品。之前我接触过很多很多的自定义switch,有些动画僵硬,有些不能自定义switch的宽度,有些只能定义宽度不能设置滑块的宽高。但,这个项目提供了各种定制的选项,堪称完美!
项目地址:https://github.com/kyleduo/SwitchButton
作者博客:http://www.kyleduo.com/
一、在xml中使用...
xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
Android开发:解决 Android N 上报错 android.os.FileUriExposedException: file:///storage/emulated/0/
2301_76222709:
Android开发:解决 Android N 上报错 android.os.FileUriExposedException: file:///storage/emulated/0/
2301_76222709:
Flutter:构建Android项目时,卡在Running Gradle task 'assembleDebug'...
woshidanoob: