层次清晰的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();
}
}