Android入门----Switch控件
2015-11-05 14:42
405 查看
Switch是Android的一个开关控件,但是该控件是4.0以后才有得,故而有些项目需要的时候不得不自己去实现该控件功能,网上主要流行的方法是继承View等控件自己在onDraw()里面绘制控件,但是不是效果不太理想就是体验性太差,另外也有修改官方Switch控件的,个人觉得修改官方Switch控件比较靠谱,比较体验性方面性能方面都有保证
一 、使用Switch控件自带的开关效果
效果图:activity_main.xml中的代码:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch控件" /> </LinearLayout></span>
MainActivity.java中的代码:
<span style="font-size:18px;">package com.test.switchdemo; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Switch; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Switch switch1 = (Switch) findViewById(R.id.switch1); switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (buttonView.isChecked()) { Toast.makeText(MainActivity.this, "开始" + buttonView.getText().toString(), Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "关闭" + buttonView.getText().toString(), Toast.LENGTH_LONG).show(); } } }); } }</span>
二、自定义Switch控件的开关效果
效果图:
activity_main.xml中的代码:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch控件" android:thumb="@drawable/thumb_selctor" android:track="@drawable/track_selctor" /> </LinearLayout></span>
增加了两个属性thumb和track,解释如下:
android:track:底部的图片
android:thumb:滑块的图片
drawable文件夹下面的thumb_selctor.xml中的代码:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下鼠标,显示白色的原型图片 --> <item android:drawable="@drawable/yuan_white" android:state_pressed="true"/> <!-- 松开鼠标,显示棕色的原型图片 --> <item android:drawable="@drawable/yuan_orange" android:state_pressed="false"/> </selector></span>
drawable文件夹下面的track_selctor.xml中的代码:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 蓝色的背景条,打开时显示该背景 --> <item android:drawable="@drawable/back_on" android:state_checked="true"/> <!-- 橘黄色的背景条,关闭时选择该背景 --> <item android:drawable="@drawable/back_off" android:state_checked="false"/> </selector></span>
MainActivity.java中的代码:同上
代码Demo下载点这里
相关文章推荐
- Android应用程序与SurfaceFlinger服务的关系概述和学习计划
- android中出现support中引用的类找不到
- Android学习路线
- Android 系统状态栏一体化
- Android 屏幕适配详解
- Android 应用的自动升级、更新模块的实现
- Android笔记整理之常见错误及解决方案汇总
- Android根据资源文件名获取它的资源ID 的两种方式
- Android OOM:内存管理分析和内存泄露原因总结
- Android之xml
- 开源中国 OsChina Android 客户端源码分析(11)缓存对象
- Android中字体加粗
- Android屏幕适配全攻略(最权威的官方适配指导)
- Android学习--GridView的使用
- Android系列教程之Activity的生命周期
- Android中的数据存储的五种方式
- 【Android4高级编程】(1)创建应用程序和Activity
- Android开发之onClick事件的三种写法
- 【Android开发笔记】pulltorefresh实现下拉刷新和上拉加载更多
- androidannotation study(1)---Activity, Fragment,Custom Class & Custom View