您可以通过添加自定义布局来实现这一点。
这是示例代码
首先定义选项卡的布局
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tabName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#000"
android:textStyle="bold"
android:text="TextView" />
</RelativeLayout>
然后添加该布局与您的选项卡一样多次。
//bar is your AcrionBar instance
LayoutInflater factory = LayoutInflater.from(this);
final View tabView = factory.inflate(R.layout.tab_layout, null);
TextView text = (TextView) tabView.findViewById(R.id.tabName);
text.setText("tab1");
bar.addTab(
bar.newTab().setCustomView(tabView)
.setTabListener(tabListener));
final View tabView1 = factory.inflate(R.layout.tab_layout, null);
TextView text1 = (TextView) tabView1.findViewById(R.id.tabName);
text1.setText("tab2");
bar.addTab(
bar.newTab().setCustomView(tabView1)
.setTabListener(tabListener));
final View tabView2 = factory.inflate(R.layout.tab_layout, null);
TextView text2 = (TextView) tabView2.findViewById(R.id.tabName);
text2.setText("tab3");
bar.addTab(
bar.newTab().setCustomView(tabView2)
.setTabListener(tabListener));
然后在 TabListener 中,您可以设置所选导航项文本的文本颜色。
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
final View tabView = tab.getCustomView();
TextView text = (TextView) tabView.findViewById(R.id.tabName);
text.setTextColor(getResources().getColor(android.R.color.black)); //There you have to put your inactive color
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
final View tabView = tab.getCustomView();
TextView text = (TextView) tabView.findViewById(R.id.tabName);
text.setTextColor(getResources().getColor(R.color.bgColor)); //There you have to put your active color
}
};