改變背景顏色

package com.tony.tabstudy; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 
import android.widget.TabWidget; 
 
public class TabStudyActivity extends Activity { 
    @ Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final TabHost tabHost = (TabHost)findViewById(R.id.tabHost); 
        tabHost.setup(); 
 
        TabHost.TabSpec spec = tabHost.newTabSpec("tab1"); 
        spec.setContent(R.id.tab1); 
        spec.setIndicator("主頁"); 
        tabHost.addTab(spec); 
 
        TabHost.TabSpec spec2 = tabHost.newTabSpec("tab2"); 
        spec2.setContent(R.id.tab2); 
        spec2.setIndicator("主頁2"
                           getResources().getDrawable(android.R.drawable.btn_dialog)); 
        tabHost.addTab(spec2); 
 
        tabHost.setCurrentTab(1); 
 
        // 初始化設置一次標籤背景 
        updateTabBackground(tabHost); 
 
        // 選擇時背景更改。 
        tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
            @ Override 
            public void onTabChanged(String tabId) { 
                updateTabBackground(tabHost); 
            } 
        }); 
    } 
 
    /** 
     * 更新Tab標籤的背景圖 
     * 
     * @param tabHost 
     */ 
    private void updateTabBackground(final TabHost tabHost) { 
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
            View vvv = tabHost.getTabWidget().getChildAt(i); 
            if (tabHost.getCurrentTab() == i) { 
                // 選中後的背景 
                vvv.setBackgroundDrawable(getResources().getDrawable( 
                                              android.R.drawable.spinner_background)); 
            } else { 
                // 非選擇的背景 
                vvv.setBackgroundDrawable(getResources().getDrawable( 
                                              R.drawable.a)); 
            } 
        } 
    } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章