安卓Android的TabActivity/TabHost实现每个Tab一个Activity(图标+文字)
根据网上资料,写了一个通用的类似模板的类MyTabActivity,实现一个图标Icon+文字Label的TabActivity,应用的类只要把Icon和Label加上去,对应每个Activity,就可以建立一个TabActivity了。
1. 模板类MyTabActivity.java(可以重用)
[java]
package amao.callbye;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
/**
* Abstract TabActivity with icon+text TabSpec support for each Activity
* Sub class need set "layout" and "selectDrawable"(tab selected background image) in constructor
* And implement getMyTabList() to add tab configuration
*
* @author Anderson Mao, 2012-05-01
*/
public abstract class MyTabActivity extends TabActivity {
private static String TAG_NAME = MyTabActivity.class.getSimpleName();
private TabHost tabHost;
private int tabLayout;
private int selectDrawable;
private Drawable selectBackground;
private int textColor = Color.GRAY;
private int selectTextColor = Color.LTGRAY;
private Map<String, TabView> tabViewMap = new HashMap<String, TabView>();
private String tabViewTagPrev=null;
public abstract List<MyTab> getMyTabList();
public MyTabActivity(int tabLayout, int selectDrawable){
this.tabLayout = tabLayout;
this.selectDrawable = selectDrawable;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(tabLayout);
tabHost = getTabHost(); // Get TabHost after setContentView()
//
initTabHost();
}
private void initTabHost(){
selectBackground = this.getResources().getDrawable(selectDrawable);
int index = 0;
// Create TabSpec for each MyTab. The first tab is the default
String defaultTag = null;
TabView defaultTabView = null;
List<MyTab> myTabList = getMyTabList();
for(MyTab myTab: myTabList){
index++;
String tag = Integer.toString(index);
TabView view = new TabView(this, myTab.icon, myTab.text);
TabSpec tabSpec = tabHost.newTabSpec(tag)
.setIndicator(view)
.setContent(new Intent(this, myTab.activity))
;
tabViewMap.put(tag, view);
tabHost.addTab(tabSpec);
if(defaultTag == null){
defaultTag = tag;
defaultTabView = view;
}
}
// Listener on tab change
tabHost.setOnTabChangedListener( new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId){
Log.d(TAG_NAME,"change tab: id="+tabId+", prevId="+tabViewTagPrev);
if(tabViewTagPrev!=null){
// Reset prev tab
TabView tvPrev = tabViewMap.get(tabViewTagPrev);
if(tvPrev!=null){
tvPrev.setBackgroundDrawable(null);
tvPrev.textView.setTextColor(textColor);
}
}
// Set current selected tab
&nbs
补充:移动开发 , Android ,