7

我正在尝试按照参考自定义 ActionBar 的选项卡指示器,但是出现错误:

 Error retrieving parent for item: No resource found that matches the given name '@style/Widget.Holo.ActionBar.TabView'.    

最低 SDK 设置为 14,目标 SDK = 18。有什么想法吗?

编辑:

我已经有以下可用的样式:

     <style name="sMain" parent="@android:style/Theme.Holo">
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarDivider">@null</item>
</style>

<style name="MyActionBar" parent="android:Widget.Holo.ActionBar">
    <item name="android:actionBarDivider">@null</item>

</style>
4

2 回答 2

31

你应该参考

@android:style/Widget.Holo.ActionBar.TabView

文档中的错字 - 他们放弃了android:.

于 2013-08-21T19:15:02.983 回答
1

This is what you need to do:

Create a Style for your app: Here I am customizing the Tab Bar and it's text (I am using a base compatibility theme but you can use HOLO or anything you like):

   <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="android:actionBarTabTextStyle">@style/TabTextStyle</item>
        <item name="android:actionBarTabStyle">@style/TabBarStyle</item>

        <!-- Support library compatibility (ActionBarCompat) -->
        <item name="actionBarTabTextStyle">@style/TabTextStyle</item>
        <item name="actionBarTabStyle">@style/TabBarStyle</item>
    </style>

Create those styles:

<style name="TabTextStyle" parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/ab_tab_txt</item>
</style>

<style name="TabBarStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_indicator</item>
</style>

For the color and drawables, you can create a selector that allows the tab to change based on clicks and selection:

File: res/color/ab_tab_txt (I am using the color file from res/values to set my constants, but you can place the color like so: #FFF)

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:color="@color/ab_tab_txt_selected"/>
        <item android:color="@color/ab_tab_txt_unselected"/>
    </selector>

File res/drawable/tab_indicator

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@android:color/transparent" />
    <item android:state_focused="false"
        android:state_selected="true" 
        android:state_pressed="false"
        android:drawable="@drawable/tab_selected_example" />

    <!-- Focused states -->
    <item android:state_focused="true"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_unselected_focused_example" />
    <item android:state_focused="true"
        android:state_selected="true" 
        android:state_pressed="false"
        android:drawable="@drawable/tab_selected_focused_example" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="true"
        android:drawable="@drawable/tab_unselected_pressed_example" />
    <item android:state_focused="false" 
        android:state_selected="true"  
        android:state_pressed="true" 
        android:drawable="@drawable/tab_selected_pressed_example" />

    <!--    Focused states -->
    <item android:state_focused="true" 
        android:state_selected="false" 
        android:state_pressed="true" 
        android:drawable="@drawable/tab_unselected_pressed_example" />
    <item android:state_focused="true" 
        android:state_selected="true"  
        android:state_pressed="true" 
        android:drawable="@drawable/tab_selected_pressed_example" />
</selector>

My drawable files are NinePatches that I create using this useful tool: http://jgilfelt.github.io/android-actionbarstylegenerator/

于 2013-08-21T17:26:07.173 回答