先来看下效果:
main.xml
tabswitcher.java
iaiaiactivity.java
tabswitcher_long.9.png:
tabswitcher_short.9.png:
main.xml
<?xml version="1.0" encoding="utf-8"?><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff"> </linearlayout></linearlayout>
tabswitcher.java
package com.iaiai.activity;import android.content.context;import android.graphics.color;import android.view.gravity;import android.view.view;import android.widget.linearlayout;import android.widget.textview;/** * * <p> * title: tabswitcher.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> * * @author 丸子 * @version 0.0.1 */public class tabswitcher extends linearlayout { private context context; private string[] texts; private int selectedposition = 0; private int oldposition = selectedposition; private int[] background = { r.drawable.tabswitcher_short, color.transparent }; private textview[] tvs; public tabswitcher(context context, string[] texts) { super(context); this.texts = texts; this.context = context; setorientation(horizontal); linearlayout.layoutparams params = new linearlayout.layoutparams( linearlayout.layoutparams.fill_parent, linearlayout.layoutparams.wrap_content); setlayoutparams(params); setbackgroundresource(r.drawable.tabswitcher_long); } @override protected void onfinishinflate() { // todo auto-generated method stub super.onfinishinflate(); tvs = new textview[texts.length]; linearlayout.layoutparams params = new linearlayout.layoutparams( linearlayout.layoutparams.fill_parent, linearlayout.layoutparams.wrap_content); params.weight = 1; params.gravity = gravity.center_vertical; for (int i = 0; i < texts.length; i++) { textview child = new textview(context); child.settag(i); child.settextsize(16); child.settextcolor(color.black); child.setgravity(gravity.center); tvs[i] = child; child.setonclicklistener(listener); this.addview(child, params); } } onclicklistener listener = new onclicklistener() { @override public void onclick(view v) { selectedposition = (integer) v.gettag(); if (selectedposition != oldposition) { tvs[oldposition].setbackgroundcolor(background[1]); oldposition = selectedposition; ((textview) v).setbackgroundresource(background[0]); if (onitemclicklisener != null) { onitemclicklisener.onitemclicklisener(v, selectedposition); } } } }; @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); oldposition = selectedposition; for (int i = 0; i < texts.length; i++) { tvs[i].settext(texts[i]); if (selectedposition == i) { tvs[i].setbackgroundresource(background[0]); } else { tvs[i].setbackgroundcolor(background[1]); } } } private onitemclicklisener onitemclicklisener; public void setonitemclicklisener(onitemclicklisener onitemclicklisener) { this.onitemclicklisener = onitemclicklisener; } public interface onitemclicklisener { void onitemclicklisener(view view, int position); } public void settexts(string[] texts) { this.texts = texts; } public void setselectedposition(int selectedposition) { this.selectedposition = selectedposition; }}
iaiaiactivity.java
package com.iaiai.activity;import android.app.activity;import android.os.bundle;import android.view.view;import android.widget.linearlayout;import android.widget.toast;import com.iaiai.activity.tabswitcher.onitemclicklisener;/** * * <p> * title: iaiaiactivity.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> * * @author 丸子 * @version 0.0.1 */public class iaiaiactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); tabswitcher tabswitcher = new tabswitcher(this, new string[] { "1", "2", "3", "4" }); tabswitcher.setselectedposition(1); tabswitcher.setonitemclicklisener(onitemclicklisener); tabswitcher.onfinishinflate(); linearlayout linearlayout = (linearlayout) findviewbyid(r.id.tab); linearlayout.addview(tabswitcher, new linearlayout.layoutparams( linearlayout.layoutparams.fill_parent, linearlayout.layoutparams.wrap_content)); } onitemclicklisener onitemclicklisener = new onitemclicklisener() { @override public void onitemclicklisener(view view, int position) { // todo auto-generated method stub // switch (position) { case 0: case 1: case 2: toast.maketext(iaiaiactivity.this, "position clicked:" + position, toast.length_short) .show(); break; default: break; } } };}
tabswitcher_long.9.png:
tabswitcher_short.9.png: