简体   繁体   English

如何设置背景颜色TabHost

[英]How to Set Background Color TabHost

I need help, I'm finding difficulty for change background color in a TabHost. 我需要帮助,我发现在TabHost中更改背景颜色有困难。

Original Image: 原始图片:

此搜索

I need to modify background color like image below. 我需要修改背景颜色,如下图所示。

图像2

I tried many things in my code and XML too, but failed. 我在我的代码和XML中尝试了很多东西,但都失败了。

My code below: 我的代码如下:

 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

in XML i have this: 在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>

Somebody have some idea i thanks a lot. 有人有一些想法,我非常感谢。

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

        }
    });

Try this method, I hope this will help you. 尝试这种方法,我希望这会对你有所帮助。

Solution is to use background with selector, and the code is here: 解决方案是使用带选择器的背景,代码在这里:

private void initTabsAppearance(TabWidget tabWidget) {
    // Change background
    for(int i=0; i < tabWidget.getChildCount(); i++)
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}

Where tab_bg is an xml drawable with selector: tab_bg是带选择器的xml drawable:


For the full Tab customization I will add the code for changing tab text style using custom theme. 对于完整的Tab自定义,我将添加使用自定义主题更改选项卡文本样式的代码。 Add this to styles.xml: 将其添加到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>

To use this theme, define it in AndroidManifest.xml: 要使用此主题,请在AndroidManifest.xml中定义它:

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

And now you have tab widgets with custom background and custom text style. 现在你有了自定义背景和自定义文本样式的标签小部件。

I am solved exactly the same problem with this method: 我用这个方法解决了完全相同的问题:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM