如何更改Android选项卡小部件的背景?

47

我的类继承自TabActivity

TabHost mTabHost =  getTabHost();

TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");

tab1 .setIndicator("title tab1");
tab2 .setIndicator("title tab2");
mTabHost.addTab(tab1);mTabHost.addTab(tab2);

TabHost.setCurrentTab(0 or 1)

有人能指导我如何更改所选选项卡的背景图像或颜色吗?

6个回答

91

这将设置您的选项卡颜色:

public static void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

如果您将其放置在onTabChangedListener()内部,则它将保持所选选项卡的正确颜色。


非常感谢,这真的帮了我很多。有没有办法在XML中实现这种方法? - teoREtik
1
XML是静态内容,仅用于活动首次启动时(布局初始化),因此不需要。 - Blundell
谢谢你的帮助。这个答案非常有用。+1 给你。加油! - user881928
我不喜欢这种方式,但我认为我们没有更好的方法。 - Leo Nguyen
如果您想使用XML文件,可以查看下面的答案,该答案以XML样式设置了选择器https://dev59.com/4HI95IYBdhLWcg3w5iU4#7453721 - Blundell
显示剩余2条评论

36

正如mbaird所提到的,更好的解决方案是使用带有选择器的background,这样你就不必检查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">

现在您拥有带有自定义背景自定义文本样式的选项卡小部件。


25
如果您注册了TabHost.OnTabChanged事件并调用mTabHost.getCurrentTabView()获取视图,那么调用view.setBackgroundResource()会发生什么呢?

2
>     TabHost mTabHost =  getTabHost();
>     
>     TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
>     TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");
>     
>     tab1.setIndicator("title tab1");
>     tab2.setIndicator("title tab2");
>     mTabHost.addTab(tab1) ;mTabHost.addTab(tab2);
>     
>     TabHost.setCurrentTab(0 or 1);


mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1selector); 

mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2selector);    

mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3selector);    

mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4selector);

使用.setBackgroundResource,tabNselector是一个XML文件 - tabNselector.xml

    <?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="false" android:drawable="@drawable/tabN"/>
   <item android:state_selected="true" android:drawable="@drawable/tabNsel"  />
</selector>

2

这个链接是否解决了你的问题?基本上是在每个选项卡视图上使用选择器调用setBackgroundDrawable方法。


0
我在XML的TabWidget元素中设置了'android:background'参数,以给所有选项卡提供通用背景。
然后我在'.setIndicator'方法中传递了从另一个XML膨胀的视图。
 View v = LayoutInflater.from(this).inflate(R.layout.tab_widget, null);
    TextView label = (TextView) v.findViewById(R.id.tabLabel);
    label.setText("Whatever");
 tab1 .setContent(v);

我觉得这是一种更好的做法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接