【问题标题】:How to Set Background Color TabHost如何设置背景颜色 TabHost
【发布时间】:2013-12-06 00:20:04
【问题描述】:

我需要帮助,我很难在 TabHost 中更改背景颜色。

原图:

我需要修改背景颜色,如下图。

我也在我的代码和 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>

有人知道我非常感谢。

【问题讨论】:

    标签: java android


    【解决方案1】:
    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
    
            }
        });
    

    试试这个方法,希望对你有帮助。

    【讨论】:

    • 我们可以在加载而不是点击时更改标签颜色
    【解决方案2】:

    解决方法是使用背景和选择器,代码在这里:

    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 可绘制对象:


    对于完整的选项卡自定义,我将添加使用自定义主题更改选项卡文本样式的代码。将此添加到 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">
    

    现在您有了带有自定义背景和自定义文本样式的标签小部件。

    【讨论】:

      【解决方案3】:

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

      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 版吗?
      • 它运行在 android 2.3.3 (root HTC Tattoo, CM7) 上。我没有在其他设备上测试。
      猜你喜欢
      • 2010-12-11
      • 2013-05-06
      • 2016-03-16
      • 2012-09-21
      • 2018-10-19
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多