自定义ActionBar TabBar(ActionBarSherlock)

6

我已经被这个问题困扰了几天。有人可以帮助我自定义ActionBar下方显示的选项卡(NavigationMode为NAVIGATION_MODE_TABS)吗?

我基本上想要改变选项卡的背景颜色和当前选中选项卡的下划线颜色。到目前为止,这是我所做的,但它并不起作用。我正在使用ActionBarSherlock

<style name="Theme.Styled" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/Widget.Theme.Styled.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.Theme.Styled.ActionBar</item>

    <item name="actionBarTabBarStyle">@style/customActionBarTabStyle</item>
    <item name="android:actionBarTabBarStyle">@style/customActionBarTabStyle</item>

    <item name="actionBarTabBarStyle">@style/customActionBarTabBarStyle</item>
    <item name="android:actionBarTabBarStyle">@style/customActionBarTabBarStyle</item>

    <item name="actionBarTabTextStyle">@style/customActionBarTabTextStyle</item>
    <item name="android:actionBarTabTextStyle">@style/customActionBarTabTextStyle</item>
</style>

<style name="customActionBarTabStyle" parent="style/Widget.Sherlock.Light.ActionBar.TabView">
    <item name="android:background">@color/red</item>

    <item name="android:textSize">12dp</item>

</style>

<style name="customActionBarTabBarStyle" parent="style/Widget.Sherlock.Light.ActionBar.TabBar">
    <item name="android:background">@color/red</item>
</style>

<style name="customActionBarTabTextStyle" parent="style/Widget.Sherlock.Light.ActionBar.TabText">
    <item name="android:titleTextStyle">@style/Theme.Styled.ActionBar.TitleTextStyle</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="Widget.Theme.Styled.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">#A9E2F3</item>
    <item name="background">#A9E2F3</item>
    <item name="android:titleTextStyle">@style/Theme.Styled.ActionBar.TitleTextStyle</item>
</style>

<style name="Theme.Styled.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="Animations" />
2个回答

17

我不确定您是否还需要此信息,但我将发布答案供其他人查看。您可以将其设置为 customActionBarTabStyle 的背景 Drawable 作为Drawable资源:

<style name="customActionBarTabStyle" parent="style/Widget.Sherlock.Light.ActionBar.TabView">
    <item name="android:background">@drawable/actionbar_tabs_selector</item>
    <item name="android:textSize">12dp</item>
</style>

这个资源应该是一个选择器,类似于这些:

<!-- This is the "@drawable/actionbar_tabs_selector" layout !-->

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/actionbar_tab_style_nselected"/>
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/actionbar_tab_style_selected"/>

    <!-- Pressed state -->
    <item android:state_pressed="true" android:drawable="@drawable/actionbar_tab_style_selected" />

    <!-- Focused state -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/actionbar_tab_style_nselected"/>
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/actionbar_tab_style_nselected"/>

</selector>

因此,这里的资源有两个层级列表。一个用于标签未被选中时,另一个用于标签被选中且活动时。因此,您可以根据所选状态设置两个层级列表。

一个单独的层级列表可能如下所示:

<!-- This is the "@drawable/actionbar_tab_style_nselected" layout !-->

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Bottom Line -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/HCL_orange" />
        </shape>
    </item>

    <!-- Tab color -->
    <item android:bottom="2dip">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

因此,第一项是您可以定义为当前选定选项卡下划线颜色的底线,第二项是整个选项卡的颜色。


2
谢谢。嗯,我也因为糟糕的文档而与Android的样式和主题斗争,这真是太可惜了... - Tooroop

3

1
这里我解释了一些关于样式化Sherlock Action的事情。http://krishnalalstha.wordpress.com/2012/12/21/styling-sherlock-actionbar-change-actionbar-color/ - Krishna Shrestha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接