关于TabActivity的使用

现在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>



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值