2

我想更改標籤的背景。我在過去的2天裏嘗試了一切,所以我決定在這裏發佈我的代碼。 ActionBar的背景設置正確,但TABS保持黑色。無法使用ActionBarSherlock更改標籤的背景

<style name="CustomActivityTheme" parent="Theme.Sherlock"> 
    <item name="android:actionBarStyle">@style/MyActionBar</item> 
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item> 
    <!-- other activity and action bar styles here --> 
</style> 

<!-- style for the action bar backgrounds --> 
<style name="MyActionBar" parent="Widget.Sherlock.ActionBar"> 
    <item name="android:background">@drawable/backgroundactionbar</item> 
</style> 


<style name="MyActionBarTabStyle" parent="Widget.Sherlock.ActionBar.TabView"> 
<item name="android:background">@drawable/backgroundactionbar</item> 
</style> 

在清單:

  <activity android:name=".FragmentActivityDashboard" 
       android:label="@string/app_name" 
       android:configChanges="keyboardHidden|orientation|screenLayout" 
       android:theme="@style/CustomActivityTheme" 
       > 
    </activity> 

回答

7

您需要聲明兩點改變。機器人:actionBarStyle和actionBarStyle

<!-- Theme For Tabs and ActionBar Background --> 
<style name="Theme.Tabs" parent="Theme.Sherlock"> 
    <item name="android:actionBarTabStyle">@style/ActionBarTab</item> 
    <item name="actionBarTabStyle">@style/ActionBarTab</item> 

    <item name="android:actionBarStyle">@style/ActionBarBlank</item> 
    <item name="actionBarStyle">@style/ActionBarBlank</item> 
</style> 

<!-- Styles for the above theme --> 
<style name="ActionBarBlank" parent="Widget.Sherlock.ActionBar"> 
    <item name="android:background">@drawable/tab_bg</item> 
    <item name="background">@drawable/tab_bg</item> 
</style> 

<style name="ActionBarTab" parent="Widget.Sherlock.ActionBar.TabView"> 
    <item name="android:background">@drawable/bg_tab</item> 
    <item name="background">@drawable/bg_tab</item> 
</style> 

注意,對於標籤的繪製在XML文件中聲明,以表示對所選選項卡單獨繪製。

這裏的bg_tab:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<!-- Selected --> 
<item android:state_selected="true" android:drawable="@drawable/tab_bg" /> 

<!-- Normal --> 
<item android:drawable="@drawable/transparent" /> 

</selector> 
+0

最後我發現了這個例子。我不明白爲什麼他們必須這麼努力地做到這一點...... – Nestor 2013-06-17 08:52:32

+0

我同意。對UI的這些調整中的一些肯定比他們更容易。 – Tastybrownies 2014-02-23 21:04:49