当前位置: 移动技术网 > IT编程>移动开发>Android > Android UI控件Switch的使用方法

Android UI控件Switch的使用方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

撸其阁,周涛的丝袜,不爱你走

在android中偶尔会用到开关,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);
        }
      }
    });
  }
}

最终效果如下图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网