main.xml
Java代码
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff">
android:id="@+id/tab" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#ffffff">
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff">
android:id="@+id/tab" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#ffffff">
TabSwitcher.java
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: 176291935@qq.com
*
*
* 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
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
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;
}
}
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: 176291935@qq.com
*
*
* 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
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
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
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: 176291935@qq.com
*
*
* 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;
}
}
};
}