Home

Awesome

Logo SwitchButton

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

Android Arsenal

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

sample

示例 APP

SampleApp

扫描二维码或点我下载

特性

使用指南

在布局中使用

<?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 那样设置状态和监听

一些可用的方法:

自定义图片资源

SwitchButton 由四张图片构成

第一种方式你可以通过 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 <sky@panpf.me>

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.