0


Android Switch开关按钮使用和自定义样式(系列教程五)

Switch开关按钮简介

Switch开关按钮是Android中的基本控件之一,其本质上也是一个按钮,具有开和关两种展示状态。

Switch开关按钮基本使用

在布局文件中定义开关按钮:

  1. <LinearLayout
  2. android:layout_width="300dp"
  3. android:layout_height="match_parent"
  4. android:layout_centerHorizontal="true"
  5. android:layout_marginTop="20dp"
  6. android:orientation="vertical">
  7. <Switch
  8. android:id="@+id/swtTest"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. </LinearLayout>

下面是开关按钮的默认样式,比较丑,我们后面自定义比较好看的开关按钮。

在Activity中使用开关按钮:

Switch开关按钮本质上也是一个按钮,也具有对onClick、onLongClick、onTouch事件的处理能力,但它又是一个特殊的按钮,拥有一个特殊的事件,可以监听开关按钮的状态变化,如下所示:

  1. public class MainActivity05 extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main_05);
  6. //根据ID获取到开关按钮
  7. Switch swtTest = findViewById(R.id.swtTest);
  8. //给开关按钮设置监听状态改变事件
  9. swtTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  10. @Override
  11. public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  12. System.out.println("开关按钮的状态 = " + b);
  13. }
  14. });
  15. }
  16. }

Switch开关按钮属性介绍

  • textOn:开关按钮打开时显示的文字。
  • textOff:开关按钮关闭时显示的文字。
  • thumb:开关按钮上原型滑块的样式,自定义样式时需要设置此样式。
  • track:开关按钮下面导轨的样式,自定义样式时需要设置此样式。
  • switchTextAppearance:设置文本的风格,可以用来设置开关两种状态下的文本样式。
  • checked:设置初始选中状态
  • showText:设置是否显示开关上的文字(android系统中默认不显示)

Switch开关按钮自定义样式

自定义样式效果图如下,下面我们一步步去实现这个样式。

  1. 定义开关按钮底部导轨的样式:drawable/track.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--开关按钮关闭时的导轨样式-->
  4. <item android:state_checked="false">
  5. <shape>
  6. <size android:height="@dimen/switchHeight" android:width="@dimen/switchWidth"></size>
  7. <corners android:radius="20dp"></corners>
  8. <solid android:color="#b353667c"></solid>
  9. <stroke android:color="#b3ffffff" android:width="1dp"></stroke>
  10. </shape>
  11. </item>
  12. <!--开关按钮打开时的导轨样式-->
  13. <item android:state_checked="true">
  14. <shape>
  15. <size android:height="@dimen/switchHeight" android:width="@dimen/switchWidth"></size>
  16. <corners android:radius="20dp"></corners>
  17. <solid android:color="#b32881d5"></solid>
  18. <stroke android:color="#b3ffffff" android:width="1dp"></stroke>
  19. </shape>
  20. </item>
  21. </selector>
  1. 定义开关按钮上滑块的样式:drawable/thumb.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_checked="false">
  4. <layer-list>
  5. <item android:width="@dimen/switchHeight" android:height="@dimen/switchHeight">
  6. <shape android:shape="oval">
  7. <!-- <solid android:color="#b3536673"></solid>-->
  8. </shape>
  9. </item>
  10. <item android:left="@dimen/thumbOffset" android:right="@dimen/thumbOffset" android:top="@dimen/thumbOffset" android:bottom="@dimen/thumbOffset">
  11. <shape android:shape="oval">
  12. <!-- <size android:height="30dp" android:width="30dp"></size>-->
  13. <solid android:color="#b3536673"></solid>
  14. <stroke android:width="1dp" android:color="#b3ffffff"></stroke>
  15. </shape>
  16. </item>
  17. </layer-list>
  18. </item>
  19. <item android:state_checked="true">
  20. <layer-list>
  21. <item android:width="@dimen/switchHeight" android:height="@dimen/switchHeight">
  22. <shape android:shape="oval">
  23. <!-- <solid android:color="#b3536673"></solid>-->
  24. </shape>
  25. </item>
  26. <item android:left="@dimen/thumbOffset" android:right="@dimen/thumbOffset" android:top="@dimen/thumbOffset" android:bottom="@dimen/thumbOffset">
  27. <shape android:shape="oval">
  28. <solid android:color="#b3536673"></solid>
  29. <stroke android:width="1dp" android:color="#b3ffffff"></stroke>
  30. </shape>
  31. </item>
  32. </layer-list>
  33. </item>
  34. </selector>
  1. 定义开关时的字体样式:values/style.xml
  1. <style name="switchStyleDefault" parent="@style/TextAppearance.AppCompat.Widget.Switch">
  2. <item name="android:textSize">10sp</item>
  3. <item name="android:textColor">#ffffff</item>
  4. </style>
  5. <style name="switchStyleCheck" parent="@style/TextAppearance.AppCompat.Widget.Switch">
  6. <item name="android:textSize">10sp</item>
  7. <item name="android:textColor">#00ff0a</item>
  8. </style>
  1. 需要在代码中根据按钮状态设置字体样式。
  1. //给开关按钮设置监听状态改变事件
  2. swtTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  3. @Override
  4. public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  5. System.out.println("开关按钮的状态 = " + b);
  6. //设置改变字体颜色
  7. swtTest.setSwitchTextAppearance(MainActivity05.this, b ? R.style.switchStyleCheck : R.style.switchStyleDefault);
  8. }
  9. });
  1. 最后,看一下布局中Switch开关按钮属性的设置,都引用了我们前面定义的样式。
  1. <Switch
  2. android:id="@+id/swtAutoModel"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_centerVertical="true"
  6. android:layout_marginLeft="10dp"
  7. android:layout_toRightOf="@id/textView5"
  8. android:showText="true"
  9. android:switchMinWidth="50dp"
  10. android:switchTextAppearance="@style/switchStyleDefault"
  11. android:textOff="异物"
  12. android:textOn="导线"
  13. android:thumb="@drawable/thumb"
  14. android:track="@drawable/track" />

至此,Switch开关按钮自定义样式已经实现。

原创不易,点个赞再走呗。。。


本文转载自: https://blog.csdn.net/qq_34215018/article/details/127690304
版权归原作者 我的宝宝最可爱 所有, 如有侵权,请联系我们删除。

“Android Switch开关按钮使用和自定义样式(系列教程五)”的评论:

还没有评论