您的位置:首页 > 移动开发 > Android开发

switch控件的使用

2017-02-13 10:25 309 查看
Android----UI控件switch选择开关使用首先switch控件是原生的控件,可以直接使用。最低要求是安卓4.0以后才能使用。原生用法也很简单,直接在xml添加switch控件就行了.属性介绍:android:textOff     setTextOff(CharSequence) 文本使用开关时未检查/“关闭”状态。android:textOnsetTextOn(CharSequence)   文本时使用的开关检查/“打开”状态。setSwitchTypeface(Typefacetf, int style) 使用指定的字体类型库内的指定类型来设置状态标签上的文字; setSwitchTypeface(Typefacetf) 使用指定字体类型库内的固有类型来设置状态标签上的文字;setChecked(booleanchecked)   改变了这个按钮的开关状态android:switchMinWidthsetSwitchMinWidth(int) :开关组件的最小宽度必须是一个dimension值,这是一个浮点数等单位的附加“14.5 sp”。
android:thumb  指定switch控件滑动按钮的资源
android:track	指定switch控件滑动的滑槽的资源
先看下系统(6.0)的switch效果
现在自己定义一个switch控件:主要用到thumb属性和track属性就行了
<RelativeLayoutandroid:layout_width="match_parent"android:clickable="true"android:layout_height="@dimen/_45dp"android:background="@drawable/white_gray_bg"android:gravity="center_vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:ellipsize="end"android:lines="1"android:gravity="center"android:paddingLeft="@dimen/_17dp"android:text="手势解锁"android:textColor="@color/font_gray_0"android:textSize="@dimen/_17sp"/><Switchandroid:id="@+id/st_gesture"android:thumb="@drawable/switch_thumb"android:track="@drawable/switch_track"android:gravity="right|center_vertical"android:paddingRight="@dimen/_17dp"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>
资源:android:thumb="@drawable/switch_thumb"滑块的资源,小球
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><sizeandroid:width="30dp"android:height="30dp"></size><solidandroid:color="@android:color/white"></solid><strokeandroid:width="1dp"android:color="#e5e5e5" /></shape>
android:track="@drawable/switch_track"	滑槽的资源
滑槽的资源分switch开时的和关闭时的资源,所以是一个选择selector文件
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_checked="true"android:drawable="@drawable/switch_track_on"></item><itemandroid:state_checked="false"android:drawable="@drawable/switch_track_off"></item></selector>
switch_track_on打开
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="#4cd964"></solid><cornersandroid:radius="32dp"></corners></shape>
switch_track_off关闭
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="@color/white"></solid><strokeandroid:width="1dp"android:color="#e5e5e5" /><cornersandroid:radius="28dp"></corners></shape>
效果图如下:
更多不同的效果就需要更换不同滑块和滑槽资源文件就行了。
switch的监听:
mSwtich.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked){//选中的处理}else{//没有选中的处理}}});
最后,发现了一个别人一个好玩的switch控件:
funswitch

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android switch