2010-01-20 116 views
47

我的類擴展擴展TabActivity如何更改Android選項卡窗口小部件的背景?

TabHost mTabHost = getTabHost(); 

TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1"); 
TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2"); 

tab1 .setIndicator("title tab1"); 
tab2 .setIndicator("title tab2"); 
mTabHost.addTab(tab1);mTabHost.addTab(tab2); 

TabHost.setCurrentTab(0 or 1) 

可有人指導我如何更改背景圖片或所選標籤的顏色?

回答

25

如果您註冊TabHost.OnTabChanged事件並調用mTabHost.getCurrentTabView()以獲取View,然後view.setBackgroundResource(),該怎麼辦?

2

請問this能解決你的問題嗎?基本上用選擇器在每個選項卡視圖上調用setBackgroundDrawable?

93

這將設置你的標籤顏色:

public static void setTabColor(TabHost tabhost) { 
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) { 
     tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected 
    } 
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected 
} 

,如果你把它放在onTabChangedListener()內,它會保持正確的顏色選定的標籤。

+0

非常感謝,這真的幫助了我。有沒有什麼辦法在XML中實現這種方法? – teoREtik 2011-04-04 07:58:20

+1

@teoREtik XML是靜態內容,僅適用於您的活動首次啓動時(佈局初始化),因此不會。 – Blundell 2011-10-23 15:55:36

+0

感謝您的幫助..這個答案是非常有用的.. +1爲.. ..乾杯.. !! – Aditya1510 2012-11-17 05:49:45

36

正如mbaird提到的,更好的解決方案是使用背景與選擇器,所以你不必檢查onTabChanged並做手動更新。最小的代碼是在這裏:

private void initTabsAppearance(TabWidget tabWidget) { 
    // Change background 
    for(int i=0; i < tabWidget.getChildCount(); i++) 
     tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg); 
} 

tab_bg是一個XML繪製與選擇:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" /> 
    <item android:drawable="@drawable/tab_bg_normal" /> 
</selector> 

對於整個標籤定製,我會添加的代碼使用自定義主題改變標籤的文本樣式。這種加入styles.xml

<resources> 

    <style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar"> 
     <item name="android:tabWidgetStyle">@style/CustomTabWidget</item> 
    </style> 

    <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget"> 
     <item name="android:textAppearance">@style/CustomTabWidgetText</item> 
    </style> 

    <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget"> 
     <item name="android:textSize">12sp</item> 
     <item name="android:textStyle">bold</item> 
    </style> 

</resources> 

要使用這個主題,在AndroidManifest.xml中定義它:

<application android:theme="@style/MyCustomTheme"> 

現在你有自定義背景自定義文本樣式標籤控件。

0

我在XML的TabWidget元素中設置了'android:background'參數,以提供所有選項卡的通用背景。

然後我在'.setIndicator'方法中傳遞了來自另一個XML的膨脹視圖。

View v = LayoutInflater.from(this).inflate(R.layout.tab_widget, null); 
    TextView label = (TextView) v.findViewById(R.id.tabLabel); 
    label.setText("Whatever"); 
tab1 .setContent(v); 

我覺得這是一個更好的方法。

2
>  TabHost mTabHost = getTabHost(); 
>  
>  TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1"); 
>  TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2"); 
>  
>  tab1.setIndicator("title tab1"); 
>  tab2.setIndicator("title tab2"); 
>  mTabHost.addTab(tab1) ;mTabHost.addTab(tab2); 
>  
>  TabHost.setCurrentTab(0 or 1); 


mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1selector); 

mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2selector);  

mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3selector);  

mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4selector); 

使用.setBackgroundResource和tabNselector是一個XML - tabNselector.xml

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="false" android:drawable="@drawable/tabN"/> 
    <item android:state_selected="true" android:drawable="@drawable/tabNsel" /> 
</selector> 
相關問題