Android TabSwitcher自定义控件

先来看下效果:
[img]http://dl.iteye.com/upload/attachment/534814/d4740eed-cc73-34ae-a09c-1de73403fa89.jpg[/img]

main.xml
		


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;/** *  * 

* title: tabswitcher.java *

*

* e-mail: [email protected] *

*

* qq: 176291935 *

*

* http: iaiai.iteye.com *

*

* create time: 2011-8-11 *

* * @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;/** *  * 

* title: iaiaiactivity.java *

*

* e-mail: [email protected] *

*

* qq: 176291935 *

*

* http: iaiai.iteye.com *

*

* create time: 2011-8-11 *

* * @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:
[img]http://dl.iteye.com/upload/attachment/534816/23c0937d-f3a6-34f3-a403-8114c59b2f36.png[/img]
tabswitcher_short.9.png:
[img]http://dl.iteye.com/upload/attachment/534818/87448dd0-f5d1-3cbe-bb9d-2e1a38419f1e.png[/img]

你可能感兴趣的:(java)