Android TabSwitcher自定义控件

系统 1757 0
    先来看下效果:
Android TabSwitcher自定义控件

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:
 

Android TabSwitcher自定义控件


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论