Android Studio中Switch控件有关 thumb 和 track 用法
•任务
•属性
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 用法的更多相关文章
- Android Studio中Switch控件有关 textOn 和 textOff 用法
•属性 textOn:控件打开时显示的文字 textOff:控件关闭时显示的文字 showText:设置是否显示开关上的文字(API 21及以上) •用法 <?xml version=" ...
- Android线程中设置控件
在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...
- android include中的控件调用
项目中经常会有一些布局是重用的,但是如何来更好的利用这些布局中的控件 转: http://zhidao.baidu.com/link?url=GU93U8Wu31dfp7mKEx52hMJkxjFLC ...
- Android Studio自定义组合控件
在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...
- visual studio中验证控件的使用
1.RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交信息. RequiredFieldValidator控件中,主要设置三个属性: (1)ErrorM ...
- Android View中的控件和监听方法...
PS:居然三天没写博客了...今天补上...东西虽多,但是都是一些基础...代码多了一些,有人可能会这样问,粘这么多代码有毛用..其实对于一个Android的初学者来说,一个完整的代码是最容易帮助理解 ...
- Android Studio 学习 - 基本控件的使用;Intent初学
Android Studio学习第三天. 今天主要学习 1. RadioButton.CheckBox.RatingBar.SeekBar等基础控件的使用. 结合Delphi中相类似的控件,在这些基本 ...
- Android Studio中Button等控件的Text中字符串默认大写的解决方法
初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
随机推荐
- LeetCode 算法面试题汇总
LeetCode 算法面试题汇总 算法面试题 https://leetcode-cn.com/problemset/algorithms/ https://leetcode-cn.com/proble ...
- free food icons
free food icons food icons Chinese foods https://www.flaticon.com/categories/food-and-restaurant htt ...
- super fast sort algorithm in js
super fast sort algorithm in js sort algorithm Promise.race (return the fast one) Async / Await // c ...
- HTML5 in depth
HTML5 in depth Content Models Web Storage web storage 存储用户信息, 替代 cookies LocalStorage SessionStorage ...
- vue页面切换过渡
<!--<transition name="slide-left">--> <router-view></router-view>& ...
- NGK DeFi Baccarat或将推动BGV成为下一个千倍币!
目前,已经接近2020年年末,但是DeFi的热潮还在持续.近日,DeFi市场传出一道重磅利好消息,便是NGK DeFi去中心化交易系统Baccarat即将上线.届时,将会引起整个区块链市场的又一次震动 ...
- [转]ROS Q&A | How to read LaserScan data
http://www.theconstructsim.com/read-laserscan-data/ Step 1. Open a project on ROS Development Studio ...
- tcpdump后台抓包并输出给wireshark
# 先进到/tmp 目录执行,方便Filezila 传输 # 开启抓包 nohup tcpdump -i eth0 -s0 -nnA 'port 22' -w dump22.pcap & [1 ...
- #progma pack(x)说明
1.字节对齐(内存相关) 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数 ...
- jQuery实现QQ简易聊天框
实现效果: html代码: <section id="chat"> <div class="chatBody"></div> ...



