2012-07-09 60 views
2

如何根据是否选择标签来更改操作栏中标签的颜色?标签夏洛克操作栏中的文字自定义颜色

它应该看起来像这样:选中时为黑色,未选中/不活动时为棕色。

我试图在styles.xml中设置它,但我找不到合适的名称来使它工作。

非常感谢您的帮助!

编辑:我使用下面的代码段用于TabsListener

class MyTabsListener implements TabListener { 
     private Fragment fragment; 

     public MyTabsListener(Fragment ef) { 
      this.fragment = ef; 
     } 

     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      ft.replace(R.id.realtabcontent, fragment); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
     } 

     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     } 

     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     } 
    } 

enter image description here

回答

8

其实这是很easy.All你应该做的是确定这样

<style name="tabtextcolor" parent="@style/Widget.Sherlock.ActionBar.TabText"> 
    <item name="android:textColor">@android:color/white</item> 
</style> 

然后

将这些样式应用到主题属性

<item name="actionBarTabTextStyle">@style/tabtextcolor</item> 
<item name="android:actionBarTabTextStyle">@style/tabtextcolor</item> 
+0

好处是,即使不使用sherlock,也可以在常规操作栏中使用相同的功能。 – SaltyNuts 2013-05-14 20:08:43

+0

在我的代码中说找不到匹配给定名称的资源'android:style/ Widget.Sherlock.ActionBar.TabText'请帮忙 – 2014-05-17 11:22:40

+0

@PatelDhaval你的项目依赖关系中有actionbarsherlock库吗?添加后,清理项目并重试 – kaplanfat 2014-05-21 07:45:15

0

如果您注册TabHost.OnTabChanged事件,并呼吁mTabHost.getCurrentTabView()来获取视图,然后view.setBackgroundResource() - 你可以通过创建像这样颜色资源设置背景图片...

+0

见上请我更新 – noloman 2012-07-09 10:51:41

+0

对不起,我不知道一个FragmentTransaction是什么....生病尝试做一些阅读,并在某些时候回复给你 – RenegadeAndy 2012-07-09 11:19:46

0
<!-- ActionBar Tab bar style --> 
     <item name="actionBarTabBarStyle">@style/Sundio.ActionBarTabs</item> 
     <item name="android:actionBarTabBarStyle">@style/Sundio.ActionBarTabs</item> 

<style name="Sundio.ActionBarTabs" parent="Widget.Sherlock.Light.ActionBar.TabBar"> 
     <item name="android:background">@drawable/actionbar_tabs_selector</item> 
     <item name="background">@drawable/actionbar_tabs_selector</item> 
     <item name="titleTextStyle">@color/brown_text_color</item> 
     <item name="android:titleTextStyle">@color/brown_text_color</item> 
    </style> 
+0

不适合我。 – 2012-10-30 05:21:58

3

你可以这样做:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true" android:color="#ff7a7a7a"/> <!-- pressed --> 
    <item android:state_focused="true" android:color="#ff7a7a7a"/> <!-- focused --> 
    <item android:state_selected="true" android:color="#ff6955b4"/> <!-- selected --> 
    <item android:color="#ff7a7a7a"/> <!-- default --> 

</selector> 

在你的风格中,你会做这样的事情。我使用ActionBarSherlock,但您可以非常容易地找到非Sherlock应用程序的TabText样式。

<style name="Theme.StyledActionBar" parent="@style/Theme.Sherlock"> 
    <item name="actionBarTabTextStyle">@style/StyledActionBarTabText</item> 
    <item name="android:actionBarTabTextStyle">@style/StyledActionBarTabText</item> 
</style> 

<style name="StyledActionBarTabText" parent="@style/Widget.Sherlock.ActionBar.TabText"> 
    <item name="android:textColor">@color/tab_text</item> 
</style> 
+0

对我不起作用,表示没有找到attr action的资源BarTabTextStyle – 2014-05-17 11:29:57

11

上面的答案是有效的,但他们是不完整。如果你想有一个改变取决于标签的状态的不同文本颜色您需要按照下面的步骤:

  1. 首先,添加以下行到res/values/colors.xml文件

    <color name="text_tab_selected">#000000</color> 
    <color name="text_tab_unselected">#886C2A</color> 
    
  2. 创建一个android xml资源文件/res/color将其称为tab_text.xml(或任何你想要的,但跟踪文件名)。这个文件需要成为@Maarek的选择器。

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.androdid.com/apk/res/android"> 
        <item android:state_selected="true" android:color="@color/text_tab_selected" /> 
        <item android:state_active="true" android:color="@color/text_tab_selected"/> 
        <item android:state_selected="false" android:color="@color/text_tab_unselected" /> 
        <item android:state_active="false" android:color="@color/text_tab_unselected"/> 
    </selector> 
    

    请注意state_active="false"state_active="true"是这里真正的交易。

  3. 正如Fatih Kaplan和noloman所解释的那样,您必须将新的样式添加到您的主题样式中。打开或创建res/values/styles.xml并添加哟你的主题下面几行:

    <style name="TabTextColor" parent="@style/Widget.Sherlock.ActionBar.TabText"> 
    <item name="android:textColor">@color/tab_text</item> 
    </style> 
    
  4. 最后添加到您的应用程序主题以下行的<style name="Theme.yourTheme" parent="@style/Theme.Sherlock">

    <item name="actionBarTabTextStyle">@style/TabTextColor</item> 
    <item name="android:actionBarTextStyle>@style/TabTextColor</item> 
    
  5. Corolary内:记住你的主题添加到manifest.xml文件中的活动。
    如果存在,请记住在每个Api版本的任何样式文件中重复步骤4。 (res/values-v11/styles.xml, res/values-v16/styles.xml and so on)
    如果你得到"android:actionBarStyle"线棉绒警告,用以下内容替换该行:

    <item name="android:actionBarTabTextStyle" tools:ignore="NewApi">@style/TabTextColor</item> 
    

Tab capture

+0

不适合我。 ( – VikramV 2014-03-12 10:36:37

+0

)你是否在每个不同的styles.xml文件上重复该过程?无论如何,ABS已被弃用,你应该使用google的appcompat lib – Imanol 2014-03-12 10:58:41

+0

不,我已经在res/values/styles.xml文件中添加了上述内容 此外,我没有使用ABS,我使用了普通的FragmentActivity类,其中每个标签都有多个片段。 – VikramV 2014-03-12 15:28:17