5

我在 Ice Cream Sandwich 应用程序中使用 TabHost/TabWidget 的折旧库。我没有时间熟悉 ActionBarSherlock,所以我不得不以我知道的唯一方式进入应用程序。

我想知道如何设置 TabWidget 及其 Tab 对象的样式,从更改选定的选项卡颜色到背景颜色和图像?使用标准样式和主题似乎不起作用。

4

1 回答 1

8

假设不推荐使用的库像往常一样工作,这就是我用来为标签着色的过程。我只是在代码中设置了背景,因为它不能在 xml 中直接访问:

 TabWidget tabs = (TabWidget)getTabWidget();            
        for (int i = 0; i<tabs.getChildCount(); i++) {
            RelativeLayout tab = (RelativeLayout) tabs.getChildAt(i);
            tab.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.tabindicator));

tabindicator drawable 如下:

<?xml version="1.0" encoding="utf-8" ?> 
<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_selected" /> 
<!--  Focused states  --> 
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
 <!--  Pressed  --> 
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_press" /> 
<item android:state_pressed="true" android:drawable="@drawable/tab_press" /> 
</selector>

可绘制对象只是带有颜色的 9-patch 图像,尽管您可以使用标准颜色获得类似的效果。

于 2012-04-11T09:36:29.073 回答