android switch 的track 和sb thumbtrack 的区别

android基本控件ToggleButton&Switch使用指南_android开发_ThinkSAAS
android基本控件ToggleButton&Switch使用指南
android基本控件ToggleButton&Switch使用指南
内容来源: 网络
ToggleButton(开关按钮)和Switch(开关)讲解:
一、核心属性讲解:
(1)ToggleButton
textOn:按钮被选中的时候文字显示
textOff:按钮没有被选中的时候文字显示
ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本。
以下案例为ToggleButton的用法
目录结构
main.xml布局文件
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&ImageView android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bulb_off" 
android:layout_gravity="center_horizontal" /&
&ToggleButton android:id="@+id/toggleButton"
android:layout_width="140dip"
android:layout_height="wrap_content"
android:textOn="开灯"
android:textOff="关灯"
android:layout_gravity="center_horizontal" /&
&/LinearLayout&
ToggleButtonActivity类
package com.ljq.
import android.app.A
import android.os.B
import poundB
import android.widget.ImageV
import android.widget.ToggleB
import poundButton.OnCheckedChangeL
public class ToggleButtonActivity extends Activity {
private ImageView imageView=
private ToggleButton toggleButton=
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(ImageView) findViewById(R.id.imageView);
toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
toggleButton.setChecked(isChecked);
imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
运行效果:
(2)switch:
showText:设置textOn/off的时候文字是否显示
android:showText:设置on/off的时候是否显示文字,boolean
android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,boolean
android:switchMinWidth:设置开关的最小宽度
android:switchPadding:设置滑块内文字的间隔
android:textOff:按钮没有被选中时显示的文字
android:textOn:按钮被选中时显示的文字
android:textStyle:文字风格,粗体,斜体写划线那些
android:track:底部的图片
android:thumb:滑块的图片
可以自己动手试一试每一个属性
在做一个蓝牙开关时候,用到了switch,记一下用法,其实跟Button是几乎一样的.
布局中:
&Switch 
android:id="@+id/open" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textOff="蓝牙关闭中" 
android:textOn="蓝牙开启中" /& 
java代码中
open.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
@Override 
public void onCheckedChanged(CompoundButton buttonView, 
boolean isChecked) { 
// TODO Auto-generated method stub 
if (isChecked) { 
mBluetoothAdapter.enable();//打开蓝牙 
} else { 
mBluetoothAdapter.disable();// 关闭蓝牙 
就是这样了,一看就明白了.
PHP开发框架
开发工具/编程工具
服务器环境Android UI(1)
自定义Switch样式
开启样式:
关闭样式:
1.1 控件属性
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="28dp"
android:textOn=""
android:textOff=""
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"/&
1. thumb属性:控制上浮圆圈。
2. track属性:控制底部滑动轨道。
2.1 switch_track_checked.xml
2.2 switch_track_unchecked.xml
2.1 switch_track.xml
3.1 switch_thumb_checked.xml
3.2 switch_thumb_unchecked.xml
3.3 switch_thumb.xml
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:8630次
排名:千里之外
原创:18篇
评论:14条
(4)(9)(1)(1)(2)(1)在某些版本的系统上,thumb显示不出来,自定义的track也显示出来,
1、查看源码后得知,在某些android系统版本上,thumb的宽度是和textOn,textOff的文字宽度是一致的,是不会重新计算thumb的drawable的宽度的。为了兼容这些系统(比如4.4.2)可以在textOn中设置一些空格,点据一定的宽度,这样thumb就有一定的宽度可以显示出来了。
android:id="@+id/message_danmu_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:clickable="true"
android:selectAllOnFocus="true"
android:showText="false"
android:switchTextAppearance="@android:style/TextAppearance.Small"
android:textOff="        "
android:textOn="        "
android:textSize="16sp"
android:thumb="@drawable/icon_barrage"
android:thumbTextPadding="0dp"
android:track="@drawable/my_switch_track"/&
2、track显示不出来,经过测试,给track中的drawable设置一下宽度就可以了
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"&
android:color="#6FD764"/&
android:radius="5dp"/&
android:width="30dp"
android:height="34dp"/&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3144次
排名:千里之外
原创:14篇
(1)(3)(1)(2)(1)(2)(3)(1)(1)(1)第五章-开关按钮ToggleButton和Switch
public abstract class
CompoundButton
extends Button
implements Checkable
java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
poundButton
Known Direct Subclasses
CheckBox, RadioButton, Switch, ToggleButton
由继承结构可以看出Switch和ToggleButton都继承自CompoundButton这一抽象类。因此它们属性和方法都是类似的,首先看一下ToggleButton的用法。
ToggleButton
常用的属性很简单就两个:
android:textOff:按钮关闭时显示的文本
android:textOn:按钮开启时显示的文本
开关监听为:setOnCheckedChangeListener,覆写onCheckedChanged(CompoundButton buttonView, boolean isChecked)方法,其中isChecked表示是否选中。
用过小米手电筒APP的用户都会对它的UI有印象,也就是开灯时,手机屏幕里的灯泡也会亮,关灯时手机屏幕里的灯泡则不会亮,下面我们就结合ToggleButton实现这一功能。
主布局文件(activity_main.xml)
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"&
&ImageView
android:id="@+id/iv"
android:layout_margin="20dp"
android:layout_gravity="center"
android:src="@drawable/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
&ToggleButton
android:id="@+id/tb"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="关灯"
android:disabledAlpha="@android:integer/config_longAnimTime"
android:textSize="25sp"
android:layout_margin="20dp"
android:textOn="开灯" /&
&/LinearLayout&
设置了一个ImageView用于显示灯泡的开关,设置了一个ToggleButton当做开关,并设置了textOff和textOn属性。
MainActivity.java代码(MainActivity.java)
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleB
private ImageView imageV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton=(ToggleButton)findViewById(R.id.tb);
imageView=(ImageView)findViewById(R.id.iv);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
imageView.setImageResource(isChecked?R.drawable.on:R.drawable.off);
这里设置了setOnCheckedChangeListener选择变化的监听,并覆写了其onCheckedChanged方法,根据参数isChecked判断要显示哪张图片。
运行实例如下:
选择关灯时灯泡显示的也是暗的,选择开灯,灯泡相应变亮。
有同学会说这样的开关按钮不够直观、不够美观,下面介绍一下如何自定义ToggleButton的外观。
首先准备两张图片:
在drawable文件下创建xml文件,代码如下:
&?xml version="1.0" encoding="utf-8"?&
&selector xmlns:android="/apk/res/android"&
&item android:drawable="@drawable/on" android:state_checked="true"/&
&item android:drawable="@drawable/off" android:state_checked="false"/&
&/selector&
设置了在android:state_checked为true的时候显示on图片,在android:state_checked为false时显示off图片。
在styles.xml文件中添加代码:
&style name="MySwitch" parent="@android:poundButton"&
&item name="android:button"&@drawable/switch_button&/item&
布局文件代码如下:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"&
&ToggleButton
android:id="@+id/sw"
style="@style/MySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#"
android:checked="true"
android:textOff="@null"
android:textOn="@null" /&
&/LinearLayout&
设置了style属性,引入我们设置的style样式,并设置textOff和textOn为@null这样就不会在图片上显示文字了。运行项目实例如下:
这时可以看到ToggleButton的外观改变了。
这里外观实现了Switch的自定义样式,功能实现了动态控制WIFI的开关。首先看一下滑块外观(switch_bg.xml):
&selector xmlns:android="/apk/res/android"&
&item android:state_enabled="true" android:drawable="@drawable/enable" /&
&item android:state_pressed="true"
android:drawable="@drawable/press" /&
&item android:state_checked="true"
android:drawable="@drawable/check_on" /&
&/selector&
这里定义了三种状态下的图片,一种是state_enable(可用状态)为true时,一种是state_pressed(按下状态时)为true,一种是state_checked(选择状态)为true。每种状态对应不同的背景图片。
对于滑动轨道的背景定义了两种不同的图片(track_bg.xml):
&selector xmlns:android="/apk/res/android"&
&item android:drawable="@drawable/track_disable" android:state_enabled="false" /&
&item android:drawable="@drawable/track_default" /&
&/selector&
一种是可用状态即默认状态下显示track_default图片,一种是不可用状态下,显示track_default图片。
主布局文件如下:
&LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"&
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="禁用Switch按钮"
android:layout_marginBottom="15dp"
android:onClick="button_click"/&
android:id="@+id/sw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:thumb="@drawable/switch_bg"
android:track="@drawable/track_bg"
android:layout_marginBottom="15dp" /&
&/LinearLayout&
定义了一个按钮Button,设置了其onClick事件用于动态控制Switch是否可用。定义了一个Switch按钮,设置了其thumb即滑块外观和track即滑动轨道外观分别为上面定义的两个图片。
MainActivity.java代码如下:
public class MainActivity extends Activity {
boolean is_enable =
WifiManager wifiM
AudioManager audioM
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn);
sw = (Switch)findViewById(R.id.sw);
sw.setChecked(true);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
public void button_click(View view){
if(is_enable == true)
is_enable =
button.setText("启用Switch按钮");
is_enable =
button.setText("禁用Switch按钮");
sw.setEnabled(is_enable);
设置了布尔型的标识位is_enable用于判断Switch的使能状态,并动态控制Button按钮上文本的显示。实现了setOnCheckedChangeListener监听,判断Switch的选择与否,根据isChecked参数进行判断是否选中。
对于开启和关闭WIFI,这里首先通过getSystemService方法并传入Context.WIFI_SERVICE常量获取WifiManager对象,调用这个对象的setWifiEnable传入true或者false就可以控制WIFI的开启或关闭了。注意这里要配置相应的权限,AndroidManifest.xml代码如下:
&?xml version="1.0" encoding="utf-8"?&
&manifest xmlns:android="/apk/res/android"
package=".switchdemo"&
&application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"&
&activity android:name=".MainActivity"&
&intent-filter&
&action android:name="android.intent.action.MAIN" /&
&category android:name="android.intent.category.LAUNCHER" /&
&/intent-filter&
&/activity&
&/application&
&uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/&
&/manifest&
配置了CHANGE_WIFI_STATE的权限。
运行项目实例如下:
第一个图片展示开启WIFI的状态,我们可以在手机状态栏中看到WIFI的状态图标,第二个图片展示关闭WIFI的状态,手机状态栏中已经看不到WIFI状态图标了,第三个图片是禁用Switch按钮的状态。android&4.0新控件Switch&自定义样式
实现完成效果图。
实现思路:
使用4.0新控件Switch,在这个上面搜的自定义样式,一个个测试shape里面的item修改完成的
activity XML的
android:id="@+id/setting_gesture_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:switchMinWidth="50dip"
//这个是控制Switch的最小宽度。默认貌似是90多,不显示字的话,比较大,修改了下
android:thumbTextPadding="15dip"
//滑动按钮不显示文字的话,宽度比较小,这个属性会增加宽度
& & android:textOn=""
& & android:textOff=""
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_track_selector" /&
后面写了半天,被吞了。。。不干了,下班
看到有三个人阅读了。。我还是抽空把剩下的贴出来吧。。
自定义样式 &
switch_thumb_selector.xml
我的需求是,不管滑动按钮在左还在右,都一个样子,如果滑动到右边变色,就把state_checked="true"里面的样式换下,state_pressed="true"
监听的是手指按上去的状态,有这个需求,也可以换样式
上面自定义样式用到的布局文件&white_thumb.xml
& & //实际是个shape
activity布局里面Switch的track自定义样式&
switch_track_selector
在上面链接搜的样式里面,滑动按钮滑动到左边,右边都是一个背景色,查看Switch源码,发现实现的有checked接口,于是尝试在track里面的样式监听checked状态,结果真可以、、
自定义shape样式 &
&blue_track.xml
自定义shape样式 &white_track.xml
贴XML代码,老是被吞,只好贴图了
switch的使用方式,参考Compounon,百度一下也很多的,就不做介绍了。。准备下班,走人
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 sb thumbtrack 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信