5

我一直在尝试几天来重写自定义选项卡样式的全息主题,但我的更改没有任何效果。为什么我的自定义'actionBarTabStyle'不覆盖默认样式/主题?

这里是我的styles.xml

<!-- the theme applied to the application or activity --> 
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo"> 
    <item name="android:tabWidgetStyle">@style/MyActionBarTabs</item> 
</style> 

<!-- ActionBar tabs styles --> 
<style name="MyActionBarTabs" parent="@android:style/Widget.Holo.ActionBar.TabView"> 

    <!-- tab indicator --> 
    <item name="android:background">@drawable/tabselector</item> 
</style>> 

这是我tabselector.xml

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

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

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

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

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

</selector> 

我已在我的活动使用TabHost标签和它的布局看起来像这样

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:orientation="horizontal" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 

     <FrameLayout 
      android:id="@+id/realtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</android.support.v4.app.FragmentTabHost> 

我遵循Android Developer Page的示例

我的标签看起来还是一样的。 我的标签指标应该是我自定义的粉红色。但是,没有变化,他们仍然是蓝色的。

注意事项:

  1. 我的清单文件引用更新的主题在我的应用程序标签
  2. 我styles.xml在每个值更新的文件夹(值,值-V11,价值观-V14)
  3. 这一切都被添加在我的应用程序的顶部的操作栏与我ic_launcher图像和标题

我缺少什么或者做错了吗?感谢任何帮助。谢谢你们!

回答

5

ActionBar中的选项卡使用与TabHost中的选项卡不同的主题。

您只需要将android:actionBarTabStyle更改为android:tabWidgetStyle即可。

完整示例

<style name="Your.Theme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
    <item name="android:tabWidgetStyle">@style/Your.TabWidget</item> 
</style> 

<style name="Your.TabWidget" parent="@android:style/Widget.Holo.TabWidget"> 
    <item name="*android:tabLayout">@layout/your_tab_layout</item> 
</style> 

<style name="Your.Tab" parent="@android:style/Widget.Holo.ActionBar.TabView"> 
    <item name="android:background">@drawable/your_tab_indicator</item> 
    <item name="android:layout_width">0dip</item> 
    <item name="android:layout_weight">1</item> 
    <item name="android:minWidth">80dip</item> 
</style> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/Your.Tab" 
    android:layout_height="?android:attr/actionBarSize" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@android:id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@android:id/title" 
     style="@android:style/Widget.Holo.ActionBar.TabText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:maxWidth="180dip" /> 

</LinearLayout> 

虽然,android:tabLayout不是公共属性(因此*)。谷歌不会推荐创建这样的风格,因此会建议动态修改指标。所以,这样的事情:

final TabWidget tabWidget = tabHost.getTabWidget(); 
    for (int i = 0; i < tabWidget.getTabCount(); i++) { 
     final View tab = tabWidget.getChildTabViewAt(i); 
     tab.setBackground(getResources().getDrawable(R.drawable.your_tab_indicator)); 
    } 

结果

Example

+0

感谢您的快速反应。做过某事。它绝对改变了标签下面的发际线......但是,蓝色的指示颜色仍然存在。我想我可以看到粉红色隐藏在蓝色下......但它仍然重叠。 –

+0

在style.xml中更新了我的代码,以在您的建议中显示编辑 –

+0

@MarkBarrasso我使用完整示例进行了编辑。 – adneal