现在TabActivity逐渐被Fragment取代,写这篇文章不是让大家来用TabActivity,但是我觉的有些时候一些小的应用用TabActivity还是比Fragment较简单一些,我也不多做介绍,毕竟有点过时了,但我还是贴出代码,做以收藏
package com.anjoyo.meituan.ui;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.anjoyo.meituan.R;
public class MainActivity extends TabActivity implements OnClickListener {
private TabHost host;
private final static String GROUPBUY_STRING = "GROUPBUY_STRING";// 首页
private final static String MERCHANT_STRING = "MERCHANT_STRING";// 地图
private final static String MYSELF_STRING = "MYSELF_STRING";// 消息
private final static String MORE_STRING = "MORE_STRING";// 我
private ImageView img_groupbuy;
private ImageView img_merchant;
private ImageView img_mine;
private ImageView img_more;
private TextView text_groupbuy;
private TextView text_merchant;
private TextView text_mine;
private TextView text_more;
private LinearLayout linearlayout_groupbuy;
private LinearLayout linearlayout_merchant;
private LinearLayout linearlayout_mine;
private LinearLayout linearlayout_more;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.initView();
host = getTabHost();
host.setup();
setGroupBuyTab();
setMerchantTab();
setMyselfTab();
setMeTab();
host.setCurrentTabByTag(GROUPBUY_STRING);// 默认团购页面
}
public void initView() {
img_groupbuy = (ImageView) findViewById(R.id.img_groupbuy);
img_merchant = (ImageView) findViewById(R.id.img_merchant);
img_mine = (ImageView) findViewById(R.id.img_mine);
img_more = (ImageView) findViewById(R.id.img_more);
img_groupbuy.setOnClickListener(this);
img_merchant.setOnClickListener(this);
img_mine.setOnClickListener(this);
img_more.setOnClickListener(this);
text_groupbuy = (TextView) findViewById(R.id.text_groupbuy);
text_merchant = (TextView) findViewById(R.id.text_merchant);
text_mine = (TextView) findViewById(R.id.text_mine);
text_more = (TextView) findViewById(R.id.text_more);
linearlayout_groupbuy = (LinearLayout) findViewById(R.id.linearlayout_groupbuy);
linearlayout_merchant = (LinearLayout) findViewById(R.id.linearlayout_merchant);
linearlayout_mine = (LinearLayout) findViewById(R.id.linearlayout_mine);
linearlayout_more = (LinearLayout) findViewById(R.id.linearlayout_more);
linearlayout_groupbuy.setOnClickListener(this);
linearlayout_merchant.setOnClickListener(this);
linearlayout_mine.setOnClickListener(this);
linearlayout_more.setOnClickListener(this);
}
private void setGroupBuyTab() {
TabSpec tabSpec = host.newTabSpec(GROUPBUY_STRING);
tabSpec.setIndicator(GROUPBUY_STRING);
Intent intent = new Intent(MainActivity.this, WzGroupBuyActivity.class);
tabSpec.setContent(intent);
host.addTab(tabSpec);
}
private void setMerchantTab() {
TabSpec tabSpec = host.newTabSpec(MERCHANT_STRING);
tabSpec.setIndicator(MERCHANT_STRING);
Intent intent = new Intent(MainActivity.this, WzMerchantActivity.class);
tabSpec.setContent(intent);
host.addTab(tabSpec);
}
private void setMyselfTab() {
TabSpec tabSpec = host.newTabSpec(MYSELF_STRING);
tabSpec.setIndicator(MYSELF_STRING);
Intent intent = new Intent(MainActivity.this, WzBlankActivity.class);
tabSpec.setContent(intent);
host.addTab(tabSpec);
}
private void setMeTab() {
TabSpec tabSpec = host.newTabSpec(MYSELF_STRING);
tabSpec.setIndicator(MYSELF_STRING);
Intent intent = new Intent(MainActivity.this, WzBlankActivity.class);
tabSpec.setContent(intent);
host.addTab(tabSpec);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.linearlayout_groupbuy:
case R.id.img_groupbuy:
host.setCurrentTabByTag(GROUPBUY_STRING);
img_groupbuy.setBackgroundResource(R.drawable.w1_16);
text_groupbuy.setTextColor(getResources().getColor(
R.color.textcolor));
img_merchant.setBackgroundResource(R.drawable.w1_19);
text_merchant.setTextColor(getResources()
.getColor(R.color.textgray));
img_mine.setBackgroundResource(R.drawable.w1_21);
text_mine.setTextColor(getResources().getColor(R.color.textgray));
img_more.setBackgroundResource(R.drawable.w1_22);
text_more.setTextColor(getResources().getColor(R.color.textgray));
break;
case R.id.linearlayout_merchant:
case R.id.img_merchant:
host.setCurrentTabByTag(MERCHANT_STRING);
img_groupbuy.setBackgroundResource(R.drawable.w1_17);
text_groupbuy.setTextColor(getResources()
.getColor(R.color.textgray));
img_merchant.setBackgroundResource(R.drawable.w1_18);
text_merchant.setTextColor(getResources().getColor(
R.color.textcolor));
img_mine.setBackgroundResource(R.drawable.w1_21);
text_mine.setTextColor(getResources().getColor(R.color.textgray));
img_more.setBackgroundResource(R.drawable.w1_22);
text_more.setTextColor(getResources().getColor(R.color.textgray));
break;
case R.id.linearlayout_mine:
case R.id.img_mine:
host.setCurrentTabByTag(MYSELF_STRING);
img_groupbuy.setBackgroundResource(R.drawable.w1_17);
text_groupbuy.setTextColor(getResources()
.getColor(R.color.textgray));
img_merchant.setBackgroundResource(R.drawable.w1_19);
text_merchant.setTextColor(getResources()
.getColor(R.color.textgray));
img_mine.setBackgroundResource(R.drawable.w1_20);
text_mine.setTextColor(getResources().getColor(R.color.textcolor));
img_more.setBackgroundResource(R.drawable.w1_22);
text_more.setTextColor(getResources().getColor(R.color.textgray));
break;
case R.id.linearlayout_more:
case R.id.img_more:
host.setCurrentTabByTag(MORE_STRING);
img_groupbuy.setBackgroundResource(R.drawable.w1_17);
text_groupbuy.setTextColor(getResources()
.getColor(R.color.textgray));
img_merchant.setBackgroundResource(R.drawable.w1_19);
text_merchant.setTextColor(getResources()
.getColor(R.color.textgray));
img_mine.setBackgroundResource(R.drawable.w1_21);
text_mine.setTextColor(getResources().getColor(R.color.textgray));
img_more.setBackgroundResource(R.drawable.w1_23);
text_more.setTextColor(getResources().getColor(R.color.textcolor));
break;
default:
break;
}
}
}
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundcolor" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:background="@color/shallowgray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
android:paddingBottom="2dp" >
<LinearLayout
android:id="@+id/linearlayout_groupbuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_groupbuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/w1_16"
android:clickable="true"
android:scaleType="matrix" />
<TextView
android:id="@+id/text_groupbuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/mian_bottom_group_buy"
android:textColor="@color/textcolor"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout_merchant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_merchant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/w1_19"
android:clickable="true"
android:scaleType="matrix" />
<TextView
android:id="@+id/text_merchant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/mian_bottom_merchant"
android:textColor="@color/textgray"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout_mine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_mine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/w1_21"
android:clickable="true"
android:scaleType="matrix" />
<TextView
android:id="@+id/text_mine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/mian_bottom_meself"
android:textColor="@color/textgray"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/w1_22"
android:clickable="true"
android:scaleType="matrix" />
<TextView
android:id="@+id/text_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/mian_bottom_more"
android:textColor="@color/textgray"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="50dip"
android:visibility="gone" />
</LinearLayout>
</TabHost>