如何设置TabHost的背景颜色

4

我需要帮助,我在TabHost中更改背景颜色时遇到了困难。

原始图片:

image1

我需要像下面的图片一样修改背景颜色。

image2

我在我的代码和XML中尝试了很多方法,但都失败了。

我的代码如下:

 TabHost tabHost = getTabHost();

        // Tab 1
        TabSpec aba1spec = tabHost.newTabSpec("Tab 1");
        // setting Title and Icon for the Tab
        tabHost.getTabWidget().setStripEnabled(false);
        aba1spec.setIndicator("",getResources().getDrawable(R.drawable.tabenviaarq));
        Intent photosIntent = new Intent(this, MainActivity.class);
        aba1spec.setContent(photosIntent);

    // Adding all TabSpec to TabHost
        tabHost.addTab(aba1spec); // Adding tab1

在 XML 中我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs"
            android:layout_alignParentTop="true"/>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="65dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="-5dp"
            android:background="#000000"/>
    </RelativeLayout>
</TabHost>

有些人对于IT技术方面有什么想法,我会非常感激。

3个回答

7
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String arg0) {
            for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
                tab.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.tab_selected); // unselected
            }
            tab.getTabWidget().getChildAt(tab.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_unselected); // selected

        }
    });

尝试这个方法,我希望这能对你有所帮助。


1
我们能否在加载时而不是点击选项卡时更改选项卡的颜色? - Vicky

7
解决方案是使用带有选择器的背景,代码如下:
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是一个具有selector特性的xml可绘制对象:


为了完全自定义Tab,我将添加代码以使用自定义主题更改选项卡文本样式。将以下内容添加到styles.xml中:

<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>

要使用此主题,请在AndroidManifest.xml中定义它:

<application android:theme="@style/MyCustomTheme">

现在您可以使用自定义背景和自定义文本样式的选项卡小部件。


2

我用这种方法解决了完全相同的问题:

private void setBackgroundColor() {
    int inactiveColor = getResources().getColor(R.color.inactive_tab);
    int activeColor = getResources().getColor(R.color.active_tab);

    // In this loop you will set the inactive tabs backgroung color
    for (int i = 0; i < tabWidget.getChildCount(); i++) {
        tabWidget.getChildAt(i).setBackgroundColor(inactiveColor);
    }

    // Here you will set the active tab background color
    tabWidget.getChildAt(tabHost.getCurrentTab()).setBackgroundColor(
            activeColor);
}

首先,感谢您的帮助。我有一个问题.. 这适用于2.3版本吗? - lfrancatto
它运行在Android 2.3.3上(已root的HTC Tattoo,CM7)。我没有在其他设备上测试过。 - Bosko Mijin

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