这是你需要做的:
为您的应用创建样式:
在这里,我正在自定义 Tab Bar 和它的文本(我使用的是基本兼容性主题,但你可以使用 HOLO 或任何你喜欢的东西):
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarTabTextStyle">@style/TabTextStyle</item>
<item name="android:actionBarTabStyle">@style/TabBarStyle</item>
<!-- Support library compatibility (ActionBarCompat) -->
<item name="actionBarTabTextStyle">@style/TabTextStyle</item>
<item name="actionBarTabStyle">@style/TabBarStyle</item>
</style>
创建这些样式:
<style name="TabTextStyle" parent="@style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">@color/ab_tab_txt</item>
</style>
<style name="TabBarStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator</item>
</style>
对于颜色和可绘制对象,您可以创建一个选择器,允许选项卡根据点击和选择进行更改:
文件:res/color/ab_tab_txt(我正在使用 res/values 中的颜色文件来设置我的常量,但您可以像这样放置颜色:#FFF)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/ab_tab_txt_selected"/>
<item android:color="@color/ab_tab_txt_unselected"/>
</selector>
文件 res/drawable/tab_indicator
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@android:color/transparent" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_selected_example" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_unselected_focused_example" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_selected_focused_example" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="true"
android:drawable="@drawable/tab_unselected_pressed_example" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/tab_selected_pressed_example" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="true"
android:drawable="@drawable/tab_unselected_pressed_example" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/tab_selected_pressed_example" />
</selector>
我的可绘制文件是我使用这个有用的工具创建的 NinePatches:
http://jgilfelt.github.io/android-actionbarstylegenerator/