为什么我的自定义“actionBarTabStyle”不覆盖默认风格/主题? [英] Why does my custom 'actionBarTabStyle' not override the default style / theme?

查看:135
本文介绍了为什么我的自定义“actionBarTabStyle”不覆盖默认风格/主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试了几天,现在覆盖的Holo主题为自定义选项卡样式,但我的改变不会产生任何影响。

下面是我的styles.xml

 <! - 应用到应用程序或活动的主题 - >
<样式名称=CustomActionBarTheme父=@安卓风格/ Theme.Holo>
    <项目名称=机器人:tabWidgetStyle> @风格/ MyActionBarTabs< /项目>
< /风格>

<! - 动作条的标签样式 - >
<样式名称=MyActionBarTabs父=@安卓风格/ Widget.Holo.ActionBar.TabView>

    <! - 选项卡指示器 - >
    <项目名称=机器人:背景> @可绘制/ tabselector< /项目>
< /样式和GT;>
 

这是我的tabselector.xml

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <! - 非重点国家 - >
    <项目机器人:可绘制=@可绘制/ tab_unselected_holo机器人:state_focused =假的Andr​​oid版本:STATE_ pressed =假的Andr​​oid版本:state_selected =FALSE/>
    <项目机器人:可绘制=@可绘制/ tab_selected_holo机器人:state_focused =假的Andr​​oid版本:STATE_ pressed =假的Andr​​oid版本:state_selected =真/>

    <! - 聚焦国家 - >
    <项目机器人:可绘制=@可绘制/ tab_unselected_focused_holo机器人:state_focused =真正的机器人:STATE_ pressed =假的Andr​​oid版本:state_selected =FALSE/>
    <项目机器人:可绘制=@可绘制/ tab_selected_focused_holo机器人:state_focused =真正的机器人:STATE_ pressed =假的Andr​​oid版本:state_selected =真/>

    <  - !pressed  - >
    <! - 非重点国家 - >
    <项目机器人:可绘制=@可绘制/ tab_unselected_ pressed_holo机器人:state_focused =假的Andr​​oid版本:STATE_ pressed =真正的机器人:state_selected =FALSE/>
    <项目机器人:可绘制=@可绘制/ tab_selected_ pressed_holo机器人:state_focused =假的Andr​​oid版本:STATE_ pressed =真正的机器人:state_selected =真/>

    <! - 聚焦国家 - >
    <项目机器人:可绘制=@可绘制/ tab_unselected_ pressed_holo机器人:state_focused =真正的机器人:STATE_ pressed =真正的机器人:state_selected =FALSE/>
    <项目机器人:可绘制=@可绘制/ tab_selected_ pressed_holo机器人:state_focused =真正的机器人:STATE_ pressed =真正的机器人:state_selected =真/>

< /选择器>
 

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

 < android.support.v4.app.FragmentTabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>

        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0
            机器人:方向=横向/>

        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =0dp
            机器人:layout_height =0dp
            机器人:layout_weight =0/>

        <的FrameLayout
            机器人:ID =@ + ID / realtabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_weight =1/>
    < / LinearLayout中>

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

我按照从 Android开发者页面

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

注意事项:

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

我在想什么或者做错了吗?鸭preciate任何帮助。谢谢大家!

解决方案

在标签中的动作条使用不同的主题比 TabHost标签

所有你需要做的是改变安卓actionBarTabStyle 机器人:tabWidgetStyle

完整的示例

 <样式名称=Your.Theme父=@安卓风格/ Theme.Holo.Light.DarkActionBar>
    <项目名称=机器人:tabWidgetStyle> @风格/ Your.TabWidget< /项目>
< /风格>

<样式名称=Your.TabWidget父=@安卓风格/ Widget.Holo.TabWidget>
    <项目名称=*安卓tabLayout> @布局/ your_tab_layout< /项目>
< /风格>

<样式名称=Your.Tab父=@安卓风格/ Widget.Holo.ActionBar.TabView>
    <项目名称=机器人:背景> @可绘制/ your_tab_indicator< /项目>
    <项目名称=机器人:layout_width> 0dip< /项目>
    <项目名称=机器人:layout_weight> 1< /项目>
    <项目名称=安卓了minWidth> 80dip< /项目>
< /风格>
 


 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    风格=@风格/ Your.Tab
    机器人:layout_height =机器人:ATTR / actionBarSize
    机器人:方向=横向>

    < ImageView的
        机器人:ID =@机器人:ID /图标
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:能见度=水涨船高/>

    <的TextView
        机器人:ID =@机器人:ID /标题
        风格=@安卓风格/ Widget.Holo.ActionBar.TabText
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:=了maxWidth180dip/>

< / LinearLayout中>
 

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

 最后TabWidget tabWidget = tabHost.getTabWidget();
    的for(int i = 0; I< tabWidget.getTabCount();我++){
        最后查看选项卡= tabWidget.getChildTabViewAt(我);
        tab.setBackground(getResources()getDrawable(R.drawable.your_tab_indicator));
    }
 

结果

I've been trying for days now to override the Holo Theme for a custom Tab Style, but my changes don't have any effect.

Here's my 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>>

This is my 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>

I've added the tabs using TabHost in my activity and its layout looks like this

<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>

I have followed the examples from the Android Developer Page

My tabs still look the same. My tab indicators are supposed to be my custom color of pink. However, there is no change, they are still blue.

Things to note:

  1. My manifest file references the updated theme in my application tags
  2. My styles.xml is updated in each value folder (values, values-v11, values-v14)
  3. All this did was add an action bar on top of my application with my ic_launcher image and title

What am I missing or doing wrong? Appreciate any help. Thank you all!

解决方案

The tabs in the ActionBar use a different theme than the tabs in TabHost.

All you need to do is change android:actionBarTabStyle to android:tabWidgetStyle.

Full example

<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>

Although, android:tabLayout isn't a public attribute (hence the *). Google wouldn't recommend creating the style like that because of this and instead would suggest modifying the indicator dynamically. So, something like this:

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

Results

这篇关于为什么我的自定义“actionBarTabStyle”不覆盖默认风格/主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆