2013-07-03 29 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页面有很多很好的资源。你应该检查那些,然后投票选出你喜欢的。 ;) –

相关问题