android switchbutton 大小,SwitchButton

本文介绍了用于Android的SwitchButton开关按钮,最低兼容Android 2.1。阐述其支持滑动切换、标题等特性,给出从JCenter导入的方法,说明了所需准备的图片,详细介绍在布局中使用及通过自定义属性设置图片的方式,还解释了属性含义和其它方法,并给出示例图片源码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6d1b4ca58a0f0f038c941fd351ada544.png SwitchButton

Android%20Arsenal-SwitchButton-green.svg?style=true

SwitchButton.svg

SwitchButton是用在Android上的开关按钮,最低兼容Android2.1

f85ac8f76b2068efcfe75a2f5d60d0c8.gif

示例APP

特性

支持滑动切换

支持标题

支持Left、Top、Right、Bottom Drawable

支持使用遮罩实现圆角按钮

使用指南(Usage Guide)

导入

从JCenter导入

dependencies{

compile 'me.xiaopan:switchbutton:lastVersionName'

}

lastVersionName:SwitchButton.svg(不带v)

最低支持Android2.2 API 7

准备图片

switch_frame.png

3c5b887e98b7be0dd1ba28d0e0256d19.png

switch_state_normal.png

f600e7996adf5d4eba56dd8b8d1b89aa.png

switch_state_disable.png

422364c70ee2782792c9dc547dcb0ece.png

switch_state_mask.png

5dce12c105e999fa2f05f34dbc692693.png

switch_slider_normal.png

a83820923579699b2ddbd3e67d944f48.png

switch_slider_disable.png

bdf07232438292b4d121c762a0cee640.png

selector_switch_state.xml

selector_switch_slider.xml

在布局中使用SwitchButton并通过自定义属性设置图片

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/switch"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:minHeight="50dp"

android:paddingLeft="30dp"

android:paddingRight="30dp"

android:text="接受推送"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

属性解释

withTextInterval:标题文字和按钮之间的距离

frameDrawable:框架图片,定义按钮的大小以及显示区域

stateDrawable:状态图片,显示开启或关闭

stateMaskDrawable:状态图片遮罩层

sliderDrawable:滑块图片

其它方法:

setDrawables(Drawable frameBitmap, Drawable stateDrawable, Drawable stateMaskDrawable, Drawable sliderDrawable):设置图片

setDrawableResIds(int frameDrawableResId, int stateDrawableResId, int stateMaskDrawableResId, int sliderDrawableResId):设置图片ID

setWithTextInterval(int withTextInterval):设置标题和按钮的间距,默认为16

setDuration(int duration):设置动画持续时间,单位毫秒,默认为200

setMinChangeDistanceScale(float minChangeDistanceScale):设置滑动有效距离比例,默认为0.2。例如按钮宽度为100,比例为0.2,那么只有当滑动距离大于等于(100*0.2)才会切换状态,否则就回滚

示例图片源码

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/white">

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:paddingTop="16dp"

android:paddingBottom="16dp">

style="@style/item"

android:id="@+id/switch_main_1"

android:text="接受推送"

android:checked="true"

android:background="@drawable/selector_preference_complete"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_2"

android:text="每日头条"

android:background="@drawable/selector_preference_header"

android:layout_marginTop="16dp"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_3"

android:text="每日热卖"

android:background="@drawable/selector_preference_center"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_4"

android:text="优惠促销"

android:background="@drawable/selector_preference_center"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_5"

android:text="偶像动态"

android:background="@drawable/selector_preference_center"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_6"

android:text="娱乐播报"

android:background="@drawable/selector_preference_footer"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_7"

android:text="天气预报"

android:background="@drawable/selector_preference_header"

android:layout_marginTop="16dp"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

style="@style/item"

android:id="@+id/switch_main_8"

android:text="系统消息"

android:background="@drawable/selector_preference_footer"

app:withTextInterval="16dp"

app:frameDrawable="@drawable/switch_frame"

app:stateDrawable="@drawable/selector_switch_state"

app:stateMaskDrawable="@drawable/switch_state_mask"

app:sliderDrawable="@drawable/selector_switch_slider"/>

License

/*

* Copyright (C) 2013 Peng fei Pan

*

* 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.

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值