这2天碰到一个问题,需要修改actionBar的tab字体颜色,
写一个ap进行测试,调用出tab
public class MainActivity extends Activity implements TabListener {
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// actionBar.setSubtitle("mytest");
actionBar.setTitle("test.com");
// actionBar.hide();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionBar.addTab(actionBar.newTab());
Tab tab = actionBar.newTab().setText("First").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Second").setTabListener(this);
actionBar.addTab(tab);
//hide the title
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
}
怎样修改字体颜色呢,自定义一个style去进行实现
但是在这里碰到了问题,找了很久,才知道需要修改哪个item
需要重写android:actionBarTabTextStyle
styles.xml
<resources>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<!-- item name="android:actionMenuTextColor">@android:color/black</item -->
<item name="android:actionBarTabTextStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">18sp</item><item name="android:textColor">#ff0000</item>
</style>
</resources>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
这样,就把tab里的字体颜色修改为了red
接下来,怎样修改tab选中和为选中的颜色,怎样去进行区别呢?
从button的选中设置中得到启发,
搜索 得到 selector
进行尝试
在res\color 中创建tab_text.xml,设置选中和未选中的颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true" android:color="#ff0000ff"/> <!-- focused -->
<item android:state_selected="true" android:color="#ffff0000"/>
<item android:state_selected="false" android:color="#ffffffff"/>
<!-- default -->
</selector>
再修改style里的颜色,<item name="android:textColor">@color/tab_text</item>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<!-- item name="android:actionMenuTextColor">@android:color/black</item -->
<item name="android:actionBarTabTextStyle">@style/MyActionBar</item>
<item name="android:actionMenuTextAppearance">@style/MenuTextStyle</item>
</style>
<style name="ActionBarTabTextTheme" parent="@style/AppTheme">
<item name="android:actionBarTabTextStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">18sp</item>
<item name="android:textColor">@color/tab_text</item>
</style>
这样就起效了