安卓Android的TabActivity/TabHost實現每個Tab一個Activity(表徵圖+文字)

來源:互聯網
上載者:User

根據網上資料,寫了一個通用的類似模板的類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 
                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)
[java] 
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)
[html] 
<?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   www.2cto.com
            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> 

 
作者:andersonmao


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.