2013-07-03 33 views
2

如何用這種類型的代碼在android中設置導航選項卡的背景顏色。 `如何在android中設置導航選項卡的背景顏色

  ActionBar bar = getSupportActionBar(); 
      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

      ActionBar.Tab tab1 = bar.newTab(); 
      ActionBar.Tab tab2 = bar.newTab(); 
      tab1.setText("Fragment A"); 
      tab2.setText("Fragment B"); 
      tab1.setTabListener(new MyTabListener()); 
      tab2.setTabListener(new MyTabListener()); 
      bar.addTab(tab1); 
      bar.addTab(tab2);` 

我沒有在xml中創建標籤。我只是想知道它是否可能在這種類型的代碼。謝謝。

+0

試試這個http://stackoverflow.com/questions/11842418/how-to-draw-on-actionbar-tab-when-using-actionbarsherlock-in-android – Youddh

+0

https://developer.android。 com/training/basics/actionbar/styling.html –

回答

3

您是否嘗試通過更改res/values/themes.xml中的tabbar樣式來設置標籤背景的樣式?以下是Theme.Sherlock.Light主題的示例。

<style name="Theme.test" parent="@style/Theme.Sherlock.Light"> 
     <item name="android:actionBarTabBarStyle">@style/Theme.test.tabbar.style</item> 
     <item name="actionBarTabBarStyle">@style/Widget.test.ActionBar.TabBar</item> 
</style> 

<style name="Theme.test.tabbar.style" parent="@style/Theme.Sherlock.Light.ActionBar"> 
    <item name="android:background">#00ff00</item> 
    <item name="background">#00ff00</item> 
</style> 

<style name="Widget.test.ActionBar.TabBar" parent="Widget.Sherlock.Light.ActionBar.TabBar"> 
    <item name="android:background">#00ff00</item> 
    <item name="background">#00ff00</item> 
</style> 

由於HoneyComb和HoneyComb設備的原因,您需要設置兩次。要使用新主題,您還需要將以下內容添加到清單文件中的應用程序標記中。

android:theme="@style/Theme.test" 
+0

感謝您的幫助:) – lhencq

1

我想通過檢查出這個其他SO後開始:Android ActionBar Tab Color但是,好像你只能設定全背景色彩&色底欄,沒有顏色的的每個標籤。

否則,如果你想去old-skool,你可以使用TabHost而不是ActionBar。這將允許您爲每個特定的選項卡設置顏色,並且您應該能夠對它們進行類似於4.2ish ActionBar的樣式設置。 '非常簡單:

tabHost.getTabWidget().getChildAt(TAB_1_INDEX).setBackgroundResource(R.drawable.tab_1_inactive); 
tabHost.getTabWidget().getChildAt(TAB_2_INDEX).setBackgroundResource(R.drawable.tab_2_inactive); 
tabHost.getTabWidget().getChildAt(TAB_3_INDEX).setBackgroundResource(R.drawable.tab_3_inactive); 
tabHost.getTabWidget().getChildAt(pos).setBackgroundResource(activeTabShape[pos]); 
+0

我實際上計劃將整個背景設置爲我的選項卡。不是每個項目/標籤。 :) – lhencq

+0

很酷,那麼我引用的SO頁面有很多很好的資源。你應該檢查那些,然後投票選出你喜歡的。 ;) –

相關問題