• This repository has been archived on 25/May/2021
  • Stars
    star
    332
  • Rank 126,957 (Top 3 %)
  • Language
    Java
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SwitchButton 是 Android 上的一个开关按钮控件 【Deprecated】【Stop maintenance】

Logo SwitchButton

【Deprecated】【Stop maintenance】停止维护了,推荐使用:https://github.com/zcweng/SwitchButton

Android Arsenal

SwitchButton 是 Android 上的一个开关按钮控件

sample

示例 APP

SampleApp

扫描二维码或点我下载

特性

  • 支持滑动切换
  • 支持标题
  • 支持 Left、Top、Right、Bottom Drawable
  • 支持使用遮罩实现圆角按钮

使用指南

在布局中使用

<?xml version="1.0" encoding="utf-8"?>
<me.panpf.switchbutton.SwitchButton
    android:id="@+id/switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="接受推送"/>

在代码中使用

SwitchButton 继承自 CompoundButton ,因此你可以像使用 CheckBox 那样设置状态和监听

一些可用的方法:

  • setDrawables(Drawable, Drawable, Drawable, Drawable):设置图片
  • setDrawableResIds(int, int, int, int):设置图片ID
  • setWithTextInterval(int):设置标题和按钮的间距,默认为 16dp
  • setDuration(int):设置动画持续时间,单位毫秒,默认为 200
  • setMinChangeDistanceScale(float):设置滑动有效距离比例,默认为 0.2。例如按钮宽度为 100,比例为 0.2,那么只有当滑动距离大于等于 (100*0.2) 才会切换状态,否则就回滚

自定义图片资源

SwitchButton 由四张图片构成

  • frameDrawable:框架图片,决定按钮的大小以及显示区域
  • stateDrawable:状态图片,显示开启或关闭状态
  • stateMaskDrawable:状态图片遮罩层,用于让状态图片按照遮罩层的形状显示
  • sliderDrawable:滑块图片

第一种方式你可以通过 SwitchButton 的自定义属性设置资源图片,如下:

<?xml version="1.0" encoding="utf-8"?>
<me.panpf.switchbutton.SwitchButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="接受推送"
    app:frameDrawable="@drawable/switch_frame"
    app:stateDrawable="@drawable/selector_switch_state"
    app:stateMaskDrawable="@drawable/switch_state_mask"
    app:sliderDrawable="@drawable/selector_switch_slider"/>

第二种方式是通过上面提到的 setDrawables(Drawable, Drawable, Drawable, Drawable) 或 setDrawableResIds(int, int, int, int) 方法设置资源图片

License

Copyright (C) 2017 Peng fei Pan <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

sketch

Sketch is a powerful and comprehensive image load library on Android, in addition to the basic functions, it also supports Jetpack Compose, GIF, SVG, video thumbnails, gesture zoom, huge images sampling, ExifInterface and other functions. Sketch 是 Android 上的一个强大且全面的图片加载库,除了基础功能外,还支持 Jetpack Compose、GIF、SVG、视频缩略图、手势缩放、超大图采样、ExifInterface 等功能。
Kotlin
1,934
star
2

spider-web-score-view

SpiderWebScoreView 是 Android 上的一个蛛网评分控件 SpiderWebScoreView Android is used on a cobweb score view
Java
640
star
3

assembly-adapter

AssemblyAdapter 是 Android 上的一个为各种 Adapter 提供开箱即用实现的库。AssemblyAdapter is a library on Android that provides out-of-the-box implementations for various Adapters.
Kotlin
173
star
4

pager-indicator

这是 Android 上的一个 ViewPager 页面指示器组件,用于标识当前显示的页面
Java
139
star
5

bintray-publish

Super easy way to publish your Android and Java artifacts to bintray.
Groovy
97
star
6

scratch-award-view

这是一个刮刮卡组件,用于实现刮奖效果
Java
52
star
7

stickyitemdecoration

RecyclerView 黏性 item 实现。RecyclerView sticky item implementation.
Kotlin
18
star
8

android-sheller

Sheller 是 Android 上的一个 shell 库,可帮助开发者方便的执行 shell 命令
Java
11
star
9

view-expander

ViewExpander 用来快速实现 View 展开关闭效果,使用非常简单,适用于所有的 View
Java
10
star
10

barcode-utils

BarcodeUtils 是一个条码解析、生成、扫描库,基于 zxing 封装,适合快速在项目中集成条码相关功能
Java
7
star
11

android-activitymonitor

Android, Activity, Monitor
Java
4
star
12

zoomimage

Android library for scaling images, supporting double-tap zoom, gesture zoom, single-finger drag, inertial swipe, location, rotate, huge image sub-sampling loading, and more. Both View and Compose are supported. 用于缩放图像的 Android 库,支持双击缩放、手势缩放、单指拖动、惯性滑动、定位、旋转、超大图采样加载等功能。支持 View 和 Compose。
Kotlin
3
star
13

tools4a

Extensions to the Android standard library and support libraries and some basic tools
Java
2
star
14

tools4k

Extensions to the Kotlin standard library
Kotlin
2
star
15

tools4j

Extensions to the Java standard library, some tool methods related to File, IO, primitive types, String, Array, and Collection
Java
2
star
16

maven-publish-gradle-plugin

Gradle plugin that configures an uploadArchives task to automatically upload all of your Java, Kotlin or Android libraries to any Maven instance.
Kotlin
2
star
17

android-liveevent

Java
1
star
18

jsonx

Extensions to the org.json standard library
Java
1
star