这期内容当中小编将会给大家带来有关如何在Android UI中使用Switch控件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
首先,在布局中添加上Switch控件:
<Switch
android:id="@+id/s_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="20dp"
android:textOn="on"
android:textOff="off"
android:thumb="@drawable/thumb"
android:track="@drawable/track" />
以下是该控件的常用属性:
textOn:控件打开时显示的文字
textOff:控件关闭时显示的文字
thumb:控件开关的图片
track:控件开关的轨迹图片
typeface:设置字体类型
switchMinWidth:开关最小宽度
switchPadding:设置开关 与文字的空白距离
switchTextAppearance:设置文本的风格
checked:设置初始选中状态
splitTrack:是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)
showText:设置是否显示开关上的文字(API 21及以上)
我们一般不会用该控件原本的样式,那么我们就需要自己修改样式了:
gray_thumb.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="40dp" android:width="40dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 变化率 -->
<gradient
android:endColor="#ffffff"
android:startColor="#ffffff" />
<stroke android:width="1dp"
android:color="#9e9e9e"/>
</shape>
green_thumb.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="40dp" android:width="40dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 变化率 -->
<gradient
android:endColor="#ffffff"
android:startColor="#ffffff" />
<stroke android:width="1dp"
android:color="#33da33"/>
</shape>
gray_track.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度 此处设置宽度无效-->
<size android:height="20dp"/>
<!-- 圆角弧度 15 -->
<corners android:radius="25dp"/>
<!-- 变化率 定义从左到右的颜色不变 -->
<gradient
android:endColor="#9e9e9e"
android:startColor="#9e9e9e" />
</shape>
green_track.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 高度40 -->
<size android:height="20dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="25dp"/>
<!-- 变化率 -->
<gradient
android:endColor="#33da33"
android:startColor="#33da33" />
</shape>
thumb.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- 设置按钮在不同状态下的时候,按钮不同的颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_thumb" />
<item android:drawable="@drawable/gray_thumb" />
</selector>
track.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- 控制Switch在不同状态下,底下下滑条的颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_track" />
<item android:drawable="@drawable/gray_track" />
</selector>
在styles.xml中添加如下style:
<style name="s_true" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">#33da33</item>
</style>
<style name="s_false" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">#9b9b9b</item>
</style>
最后,只需要将控件实例化出来进行相应操作就可以了:
MainActivity.class:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch aSwitch = (Switch) findViewById(R.id.s_v);
aSwitch.setChecked(false);
aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//控制开关字体颜色
if (b) {
aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.s_true);
}else {
aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1);
}
}
});
}
}
上述就是小编为大家分享的如何在Android UI中使用Switch控件了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。