繁体   English   中英

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

[英]Why does my custom 'actionBarTabStyle' not override the default style / theme?

我已经尝试了几天,以覆盖自定义标签样式的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>>

这是我的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开发人员页面上的示例

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

注意事项:

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

我想念什么或做错什么? 感谢任何帮助。 谢谢你们!

在该选项卡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>

<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不是公共属性(因此为*)。 因此,Google不建议创建这样的样式,而是建议动态修改指标。 因此,如下所示:

    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));
    }

结果

例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM