1、Switch中时Android中自带的开关控件:
效果如下:
2、完整代码如下所示:
android:id="@+id/main_switch" android:layout_width="@dimen/x103" android:layout_height="@dimen/x3" android:track="@drawable/setting_button_track" android:textOn="@string/version_on" android:textOff="@string/version_off" android:thumb="@drawable/setting_button_thumb" android:layout_marginLeft="@dimen/x429" android:showText="true" android:switchTextAppearance="@style/SwitchTextAppearance" android:layout_gravity="center" tools:ignore="UseSwitchCompatOrMaterialXml" /> 3、Switch的样式,文字编写都为自定义的,代码如下所示: ①setting_button_track xmlns:android="http://schemas.android.com/apk/res/android"> android:drawable="@drawable/setting_on_track" /> (1)setting_on_track > android:height="@dimen/x24"/> (2)setting_off_track android:height="@dimen/x24"/> ②setting_button_thumb xmlns:android="http://schemas.android.com/apk/res/android"> android:drawable="@drawable/setting_off" /> (1)setting_off android:shape="oval" > android:height="@dimen/x32"/> (2)setting_on android:shape="oval" > android:height="@dimen/x32"/> 4、switch中的属性 textOn:控件打开时显示的文字 textOff:控件关闭时显示的文字 thumb:控件开关的图片 track:控件开关的轨迹图片 typeface:设置字体类型 switchMinWidth:开关最小宽度 switchPadding:设置开关 与文字的空白距离 switchTextAppearance:设置文本的风格 checked:设置初始选中状态 splitTrack:是否设置一个间隙,让滑块与底部图片分隔 showText:设置是否显示开关上的文字(android系统中默认不显示) 5、文字样式 放置在value中 对于出现switch的格式出现问题,开关状态出现遮盖等信息都是因为尺寸设置不正确。 6、注意事项 1、对于自定义的switch的样式来说,想要显示文字,必须加上showtext,否则无法显示 2、对于文字的样式,使用属性来加载switchTextAppearance=“@style/SwitchTextAppearance” 3、想要自定义switch的样式,要特别注意thumb和trick两个属性 4、在日常学习过程,保持良好代码命名规范 相关阅读
发表评论