【问题标题】:Why does my custom 'actionBarTabStyle' not override the default style / theme?为什么我的自定义“actionBarTabStyle”不覆盖默认样式/主题?
【发布时间】:2014-04-01 18:13:09
【问题描述】:

我这几天一直在尝试为自定义选项卡样式覆盖 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 Developer Page中的例子进行了

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

注意事项:

  1. 我的清单文件在我的应用程序标签中引用了更新后的主题
  2. 我的 styles.xml 在每个值文件夹(值、值-v11、值-v14)中更新
  3. 所做的只是在我的应用程序顶部添加一个操作栏,其中包含我的 ic_launcher 图像和标题

我错过了什么或做错了什么?感谢任何帮助。谢谢大家!

【问题讨论】:

    标签: android android-actionbar android-tabhost android-theme android-styles


    【解决方案1】:

    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 不是公共属性(因此是 *)。因此,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));
        }
    

    结果

    【讨论】:

    • 感谢您的快速回复。做过某事。它确实改变了标签下方的细线......但是,蓝色指示器颜色仍然存在。我想我可以看到隐藏在蓝色下面的粉红色......但它仍然重叠。
    • 更新了我在 style.xml 中的代码以在您的建议中显示编辑
    • 成功了!在创建选项卡的 mainactivity.java 文件中使用我的 tabselector.xml 动态更改选项卡指示器。谢谢@adneal :)
    • @adneal 你是怎么知道这个解决方案的?我能以某种方式了解如何检查这些东西吗?
    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    相关资源
    最近更新 更多