为什么我的自定义'actionBarTabStyle'不会覆盖默认样式/主题?

Mar*_*sso 5 android android-tabhost android-theme android-actionbar android-styles

我已经尝试了几天来覆盖自定义标签样式的Holo主题,但我的更改没有任何效果.

这是我的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>>
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

我在我的活动中使用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>
Run Code Online (Sandbox Code Playgroud)

我已经按照Android Developer Page中的示例进行了操作

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

注意事项:

  1. 我的清单文件引用了我的应用程序标记中的更新主题
  2. 我的styles.xml在每个值文件夹中更新(values,values-v11,values-v14)
  3. 所有这一切都是在我的应用程序顶部使用ic_launcher图像和标题添加操作栏

我错过了什么或做错了什么?感谢任何帮助.谢谢你们!

adn*_*eal 5

在这些选项卡ActionBar使用不同的主题比标签TabHost.

所有你需要做的是改变android:actionBarTabStyleandroid: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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

虽然,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));
    }
Run Code Online (Sandbox Code Playgroud)

结果

例