一种方法是Selector
对选项卡状态使用自定义,无论选项卡是否具有焦点。这可能看起来像这样:
<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="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>
您将其添加到您的可绘制目录中,然后配置您的 tabwidget 以在您的活动中使用它(就像这样):
TabWidget tabWidget = getTabWidget();
for(int i = 0; i < tabWidget.getChildCount(); i++) {
RelativeLayout tabLayout = (RelativeLayout) tabWidget.getChildAt(i);
tabLayout.setBackgroundDrawable(res.getDrawable(R.drawable.tab_indicator));
}
更详细的指南在这里。看看这是否适合你。