根据网上资料,写了一个通用的类似模板的类MyTabActivity,实现一个图标Icon+文字Label的TabActivity,应用的类只要把Icon和Label加上去,对应每个Activity,就可以建立一个TabActivity了。
1. 模板类MyTabActivity.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
- TabView tv = tabViewMap.get(tabId);
- if(tv!=null){
- tv.setBackgroundDrawable(selectBackground);
- tv.textView.setTextColor(selectTextColor);
- }
- //
- tabViewTagPrev = tabId;
- }
- });
- // Set default tab
- if(defaultTag != null){
- defaultTabView.setBackgroundDrawable(selectBackground);
- defaultTabView.textView.setTextColor(selectTextColor);
- tabViewTagPrev = defaultTag;
- }
- }
- /**
- * Layout for each TabSpec
- */
- private class TabView extends LinearLayout {
- private ImageView imageView ;
- private TextView textView;
- public TabView(Context c, int icon, String text) {
- super(c);
- setOrientation(VERTICAL);
- setGravity(Gravity.CENTER);
- imageView = new ImageView(c);
- imageView.setImageDrawable(this.getResources().getDrawable(icon));
- imageView.setBackgroundColor(Color.TRANSPARENT);
- addView(imageView);
- textView = new TextView(c);
- textView.setText(text);
- textView.setTextColor(textColor);
- textView.setGravity(Gravity.CENTER);
- addView(textView);
- }
- }
- /**
- * Options for each TabSpec. Icon + Text + Activity
- */
- public class MyTab{
- private int icon;
- private String text;
- private Class<? extends Activity> activity;
- public MyTab(int icon, String text, Class<? extends Activity> activity){
- this.icon = icon;
- this.text = text;
- this.activity = activity;
- }
- }
- }
2. 实际应用类CallbyeTabActivity.java(使用MyTabActivity)
- package amao.callbye;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * TabActivity by configuration
- * @see MyTabActivity.java
- *
- * @author Anderson Mao, 2012-05-01
- */
- public class CallbyeTabActivity extends MyTabActivity {
- public CallbyeTabActivity(){
- super(R.layout.tab, R.drawable.icon_tab_select);
- }
- @Override
- public List<MyTab> getMyTabList(){
- List<MyTab> myTabList = new ArrayList<MyTab>();
- //
- myTabList.add(new MyTab(R.drawable.icon_home, getString(R.string.tab_home), CallbyeActivity.class) );
- myTabList.add(new MyTab(R.drawable.icon_setting, getString(R.string.tab_setting), SettingActivity.class) );
- myTabList.add(new MyTab(R.drawable.icon_help, getString(R.string.tab_help), HelpActivity.class) );
- myTabList.add(new MyTab(R.drawable.icon_history, getString(R.string.tab_info), InfoActivity.class) );
- //
- return myTabList;
- }
- }
3. Layout (tab.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@android:id/tabhost"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:padding="0dip"
- >
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@android:id/tabcontent"
- >
- </FrameLayout>
- <TabWidget
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:id="@android:id/tabs"
- android:background= "@android:color/black"
- android:layout_alignBottom = "@android:id/tabcontent"
- />
- </RelativeLayout>
- </TabHost>
4. 参考:
截图ScreenShoot: http://ww1.sinaimg.cn/bmiddle/827d0836gw1dt42i7tim0j.jpg
http://blog.csdn.net/andersonmao/article/details/7722242