•任务

  

•属性

  • android:track:底部的图片(灰->绿)

  • android:thumb:设置 Switch 上面滑动的滑块,也就是上图中的白色圆形滑块

•switch_thumb

  点击 app/src/res 找到 drawable 文件夹,右击->New->Drawable Resource File;

  

  在该文件中添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> <size
android:width="40dp"
android:height="40dp"/>
<solid
android:color="@color/white"/>
</shape>

  该代码完成了上图中白色的圆圈部分。

•switch_track

  接着新建一个 Drawable Resource File 文件。

  

  在该文件下添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false">
<shape android:shape="rectangle">
<solid
android:color="@color/gray"/>
<corners
android:radius="30dp"/>
</shape>
</item> <item android:state_checked="true">
<shape android:shape="rectangle">
<solid
android:color="@color/green"/>
<corners
android:radius="30dp"/>
</shape>
</item>
</selector>

  @color/gray 和 @color/green 我定义在了 color 下,分别代表灰色和绿色。

  • android:state_checked="false" : 定义了 Switch 在未点击状态下的界面显示效果,灰色的圆角矩形
  • android:state_checked="true" : 定义了 Switch 再点击状态下的界面显示效果,绿色的圆角矩形

•main_activity.xml

  修改 main_activity.xml 代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="@color/teal_700"> <ImageView
android:id="@+id/mImgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"/> <Switch
android:id="@+id/mSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textOn="开灯"
android:textOff="关灯"
android:showText="true"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
/> </RelativeLayout>

  我在该布局中放置了两个控件:ImageView 和 Switch,其中 ImageViewe 用于放置灯泡;

•为 Switch 设置点击事件

  修改 MainActivity.java 中的代码,如下所示:

public class SwitchActivity extends AppCompatActivity {

    private Switch mSwitch;
private ImageView mImgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch); mSwitch = findViewById(R.id.mSwitch);
mImgView = findViewById(R.id.mImgView);
mImgView.setImageResource(R.drawable.take_off); mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mImgView.setImageResource(isChecked ? R.drawable.take_on:R.drawable.take_off);
}
});
}
}

  通过 findViewById() 找到 mSwitch 和 mImgView 对应的控件;

  然后,对 mSwitch 设置点击事件  mSwitch.setOnCheckedChangeListener ;

  然后通过 isChecked 这个变量判断 Switch 处于何种状态:

  • isChecked = true : 为 ImageView 设置 take_on 图片
  • isChecked = false : 为 ImageView 设置 take_off 图片

  take_on 和 take_off 是我从网上下载的图标,我放在了 drawable 文件夹下:

      

Android Studio中Switch控件有关 thumb 和 track 用法的更多相关文章

  1. Android Studio中Switch控件有关 textOn 和 textOff 用法

    •属性 textOn:控件打开时显示的文字 textOff:控件关闭时显示的文字 showText:设置是否显示开关上的文字(API 21及以上) •用法 <?xml version=" ...

  2. Android线程中设置控件

    在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...

  3. android include中的控件调用

    项目中经常会有一些布局是重用的,但是如何来更好的利用这些布局中的控件 转: http://zhidao.baidu.com/link?url=GU93U8Wu31dfp7mKEx52hMJkxjFLC ...

  4. Android Studio自定义组合控件

    在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...

  5. visual studio中验证控件的使用

    1.RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交信息. RequiredFieldValidator控件中,主要设置三个属性: (1)ErrorM ...

  6. Android View中的控件和监听方法...

    PS:居然三天没写博客了...今天补上...东西虽多,但是都是一些基础...代码多了一些,有人可能会这样问,粘这么多代码有毛用..其实对于一个Android的初学者来说,一个完整的代码是最容易帮助理解 ...

  7. Android Studio 学习 - 基本控件的使用;Intent初学

    Android Studio学习第三天. 今天主要学习 1. RadioButton.CheckBox.RatingBar.SeekBar等基础控件的使用. 结合Delphi中相类似的控件,在这些基本 ...

  8. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  9. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

随机推荐

  1. CSS3实现 垂直居中 水平居中 的技巧

    1 1 1 How To Center Anything With CSS Front End posted by Code My Views 1 Recently, we took a dive i ...

  2. React Hooks: useMemo All In One

    React Hooks: useMemo All In One useMemo https://reactjs.org/docs/hooks-reference.html#usememo refs x ...

  3. vue-parent-child-lifecycle-order

    vue-parent-child-lifecycle-order vue parent child lifecycle order live demo https://99kyx.csb.app/ h ...

  4. dark theme website

    dark theme website css var dark theme prefers-color-scheme https://developer.mozilla.org/en-US/docs/ ...

  5. 2019 front-end job Interview

    2019 front-end job Interview 2019 前端面试题 掘金 https://juejin.im/tag/面试 https://juejin.im/post/5c875791e ...

  6. Python序列之列表(一)

    在Python中,列表是一种常用的序列,接下来我来讲一下关于Python中列表的知识. 列表的创建 Python中有多种创建列表的方式 1.使用赋值运算符直接赋值创建列表 在创建列表时,我们直接使用赋 ...

  7. CentOS7上安装伪分布式Hadoop

    1.下载安装包 下载hadoop安装包 官网地址:https://hadoop.apache.org/releases.html 版本:建议使用hadoop-2.7.3.tar.gz 系统环境:Cen ...

  8. cxf实例异常

    基于CXF2.3.0 Caused by: java.lang.InstantiationException: org.apache.cxf.wstx_msv_validation.WoodstoxV ...

  9. Apache支持Vue router使用 HTML5History 模式

    一.前言 前端Vue router 使用history模式,URL会比hash模式好看,这种模式要玩好,还需要后端配置支持,否则会报404错误. 注:1.前端代码省略. 2.此处后台使用Apache服 ...

  10. `curl -L` 解决 GitHub 的 raw.githubusercontent.com 无法连接问题

    解决 GitHub 的 raw.githubusercontent.com 无法连接问题 在使用 curl 下载文件时,如果出现以下情况 curl: (7) Failed to connect to ...