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中的代码:同上