我怎样才能改变取决于操作栏选项卡的颜色标签是否被选中或不?
它应该是这样的:黑色选择时,和那种棕色未选中时/无效。
我试着将它设置在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) {
}
}
事实上,它是相当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>
上述问题的答案是有效的,但他们是不完整的 。 如果你想有不同的文本颜色取决于您需要按照下面的步骤标签的状态是变化:
首先,添加以下行到res/values/colors.xml
文件
<color name="text_tab_selected">#000000</color> <color name="text_tab_unselected">#886C2A</color>
创建下一个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"
是这里真正的交易。
正如法提赫卡普兰和noloman解释,你有新的样式添加到您的主题风格。 打开或创建res/values/styles.xml
并添加哟你的主题下面几行:
<style name="TabTextColor" parent="@style/Widget.Sherlock.ActionBar.TabText"> <item name="android:textColor">@color/tab_text</item> </style>
最后添加到您的应用程序主题以下行的内部<style name="Theme.yourTheme" parent="@style/Theme.Sherlock">
<item name="actionBarTabTextStyle">@style/TabTextColor</item> <item name="android:actionBarTextStyle>@style/TabTextColor</item>
Corolary:记住你的主题添加到活动中manifest.xml文件。
请记住,如果它存在,以便重复步骤4中的任何样式文件中为每个API版本。 (res/values-v11/styles.xml, res/values-v16/styles.xml and so on)
。
如果你在一个lint警告的"android:actionBarStyle"
行中,用下面的一行:
<item name="android:actionBarTabTextStyle" tools:ignore="NewApi">@style/TabTextColor</item>
您可以通过创建像这样颜色资源做到这一点:
<?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,但你可以很容易地找到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>
如果您注册TabHost.OnTabChanged事件,并呼吁mTabHost.getCurrentTabView()来获取视图,然后view.setBackgroundResource() - 你可以设置背景图片...
<!-- 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>