架构级TabHost

  层次清晰的TabHost,绝对可以架构了!

  

package com.xxt.tabh;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class BasicTabActivity extends TabActivity {
	private int curTab=0;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	/**
	 * 为tabHost添加tabView
	 * 
	 * @param tagName
	 *            tag的名字
	 * @param drawIcon
	 *            图片的id
	 * @param context
	 *            所对应的类
	 */
	protected void addTabView(String tagName, int drawIcon, Class<?> context) {
		TabHost tabHost = this.getTabHost();
		TabSpec tabSpec = tabHost.newTabSpec(tagName);
		tabSpec.setIndicator(getView(tagName,drawIcon));
		tabSpec.setContent(new Intent(this, context));
		tabHost.addTab(tabSpec);
	}

	/**
	 * 获取一个自定义的View,View里只有一个组件ImageView
	 * 
	 * @param id
	 *            int 图片的id
	 * @return
	 */
	protected View getView(String name,int id) {
		View view = LayoutInflater.from(this).inflate(R.layout.lab_item_view,
				null);
		ImageView imageView = (ImageView) view.findViewById(R.id.lab__View);
		TextView textView=(TextView) view.findViewById(R.id.lab_textView);
		textView.setText(name.trim());
		imageView.setImageResource(id);
		return view;
	}
	//激活界面的时候获取存储的tagid选中
	@Override
	protected void onResume() {
		super.onResume();
		this.getTabHost().setCurrentTab(curTab);
	}
	
	//暂停的时候获取当前选中的tag
	@Override
	protected void onPause() {
		super.onPause();
		curTab=getTabHost().getCurrentTab();
	}
	//停止的时候获取当前选中的tag
	@Override
	protected void onStop() {
		super.onStop();
		curTab=getTabHost().getCurrentTab();
	}
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值