androidAndroidUI(Switch)详解
⽬录:
1.应⽤场景与概述
2.常⽤属性
3.简单使⽤
4.更改默认Switch的样式
5.⾃定义Switch
1.应⽤场景与概述
Switch是在4.0以后推出的,所以要注意开发时的minsdk设置,google在API 21后也推出support v7 包下的SwitchCompa的Material Design 开关控件,对低版本的有了更好的的⽀持。其实switch的应⽤场景和ToggleButton类似,多应⽤于两种状态的切换。
2.常⽤属性
android:typeface="normal":设置字体类型
android:track="":设置开关的轨迹图⽚
android:textOff="开":设置开关checked的⽂字
android:textOn="关":设置开关关闭时的⽂字
android:thumb="":设置开关的图⽚
android:switchMinWidth="":开关最⼩宽度
android:switchPadding="":设置开关 与⽂字的空⽩距离
android:switchTextAppearance="":设置⽂本的风格
android:checked="":设置初始选中状态
android:splitTrack="true":是否设置⼀个间隙,让滑块与底部图⽚分隔(API 21及以上)
android:showText="true":设置是否显⽰开关上的⽂字(API 21及以上)
3.简单使⽤
3.1)主布局
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="schemas.android/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ample.aswitch.MainActivity">
android:typeface="normal":设置字体类型
android:track="":设置开关的轨迹
android:textOff="开":设置开关checked的⽂字
android:textOn="关":设置开关关闭时的⽂字
android:thumb="":设置开关的图⽚
android:switchMinWidth="":开关最⼩宽度
android:switchPadding="":设置开关 与⽂字的空⽩距离
android:switchTextAppearance="":设置⽂本的风格
android:checked="":设置初始选中状态
android:splitTrack="true":是否设置⼀个间隙,让滑块与底部图⽚分隔-->
android:id="@+id/switch_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch:"/>
android:layout_marginTop="10dp"
android:layout_below="@+id/switch_tv"
android:id="@+id/switch1"
android:typeface="normal"
android:textOff="开"
android:textOn="关"
android:switchMinWidth="40dp"
android:switchPadding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:id="@+id/text"
android:layout_marginTop="10dp"
android:layout_below="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello World!"/>
android:layout_below="@+id/text"
android:id="@+id/switch_compat_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="switchCompat:"/>
android:layout_marginTop="10dp"
android:layout_below="@+id/switch_compat_tv" android:id="@+id/switch_compat"
android:typeface="normal"
android:switchMinWidth="40dp"
android:switchPadding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:id="@+id/text1"
android:layout_marginTop="10dp"
android:layout_below="@+id/switch_compat" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello World!"/>
3.2)主布局java类
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.support.v7.widget.SwitchCompat;
importandroid.widget.CompoundButton;
importandroid.widget.Switch;
importandroid.widget.TextView; publicclassMainActivityextendsAppCompatActivityimplementsCompoundButton.OnCheckedChangeListener{ privateSwitch aSwitch;
privateSwitchCompat aSwitchCompat;
privateTextView text1,text2,switchText,switchCompatText;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
简述android概述
setContentView(R.layout.activity_main);
//实例化
aSwitch = (Switch) findViewById(R.id.switch1);
aSwitchCompat = (SwitchCompat) findViewById(R.id.switch_compat);
text1 = (TextView) findViewById();
text2 = (TextView) findViewById(1);
//设置Switch事件监听
aSwitch.setOnCheckedChangeListener(this);
aSwitchCompat.setOnCheckedChangeListener(this);
}
/*
继承的接⼝并实现onCheckedChanged⽅法
* */
@Override
publicvoidonCheckedChanged(CompoundButton buttonView,booleanisChecked) {
Id()){
caseR.id.switch1:
if(isChecked){
text1.setText("开");
}else{
text1.setText("关");
}
break;
caseR.id.switch_compat: if(isChecked){
text2.setText("开");
}else{
text2.setText("关");
}
break;
default:
break;
}
}
}
3.3)截图效果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。