正如mbaird提到的,更好的解决方案是使用背景与选择器,所以你不必检查onTabChanged
并做手动更新。最小的代码是在这里:
private void initTabsAppearance(TabWidget tabWidget) {
// Change background
for(int i=0; i < tabWidget.getChildCount(); i++)
tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}
凡tab_bg
是一个XML绘制与选择:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" />
<item android:drawable="@drawable/tab_bg_normal" />
</selector>
对于整个标签定制,我会添加的代码使用自定义主题改变标签的文本样式。这种加入styles.xml
:
<resources>
<style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar">
<item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
</style>
<style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
<item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>
<style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
要使用这个主题,在AndroidManifest.xml中定义它:
<application android:theme="@style/MyCustomTheme">
现在你有自定义背景和自定义文本样式标签控件。
非常感谢,这真的帮助了我。有没有什么办法在XML中实现这种方法? – teoREtik 2011-04-04 07:58:20
@teoREtik XML是静态内容,仅适用于您的活动首次启动时(布局初始化),因此不会。 – Blundell 2011-10-23 15:55:36
感谢您的帮助..这个答案是非常有用的.. +1为.. ..干杯.. !! – Aditya1510 2012-11-17 05:49:45