先来看下效果:
[img]http://dl.iteye.com/upload/attachment/534814/d4740eed-cc73-34ae-a09c-1de73403fa89.jpg[/img]
main.xml
TabSwitcher.java
IaiaiActivity.java
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]
[img]http://dl.iteye.com/upload/attachment/534814/d4740eed-cc73-34ae-a09c-1de73403fa89.jpg[/img]
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:
[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]