【问题标题】:Impossible to change the background of tabs with ActionBarSherlock无法使用 ActionBarSherlock 更改选项卡的背景
【发布时间】:2012-07-17 10:18:08
【问题描述】:

我想更改标签的背景。在过去的两天里,我尝试了所有方法,所以我决定在这里发布我的代码。 ActionBar 的背景设置正确,但 TABS 保持黑色。

  <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>

【问题讨论】:

    标签: android tabs android-fragments android-actionbar actionbarsherlock


    【解决方案1】:

    你需要声明两件事来改变。 android: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>
    

    【讨论】:

    • 终于找到了这个例子。我不明白他们为什么要如此痛苦地这样做......
    • 我同意。其中一些 UI 调整肯定比现在更容易。
    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多