0

我正在关注如何制作选项卡布局的本教程。我想改变标签的背景,所以我找到了怎么做。问题是选项卡下方有一条线,仅在有选定项目时才会更改(如下图所示)。

有没有办法设置那条线的颜色?

在此处输入图像描述

活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initAcitivity();
}

private void initAcitivity() {
    // TODO Auto-generated method stub
    Resources ressources = getResources(); 
    final TabHost tabHost = getTabHost(); 

    // Android tab
    Intent intentHome = new Intent().setClass(this, HomeActivity.class);
    TabSpec tabSpecHome = tabHost
      .newTabSpec("Home")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
      .setContent(intentHome);

    // Apple tab
    Intent intentCercano = new Intent().setClass(this, CercanoActivity.class);
    TabSpec tabSpecCercano = tabHost
      .newTabSpec("Cercano")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
      .setContent(intentCercano);

    // Windows tab
    Intent intentRubro = new Intent().setClass(this, RubroActivity.class);
    TabSpec tabSpecRubro = tabHost
      .newTabSpec("Rubro")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
      .setContent(intentRubro);

    // Blackberry tab
    Intent intentBuscar = new Intent().setClass(this, BuscarActivity.class);
    TabSpec tabSpecBuscar = tabHost
      .newTabSpec("Buscar")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
      .setContent(intentBuscar);

    // add all tabs 
    tabHost.addTab(tabSpecHome);
    tabHost.addTab(tabSpecCercano);
    tabHost.addTab(tabSpecRubro);
    tabHost.addTab(tabSpecBuscar);

    setTabColor(tabHost);

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            setTabColor(tabHost);
        }
    });
    //set Windows tab as default (zero based)
    tabHost.setCurrentTab(0);
}

public void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
    {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(this.getResources().getColor(R.color.background_barra_unselected)); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(this.getResources().getColor(R.color.background_barra_selected)); // selected
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             >

            <LinearLayout
                android:id="@+id/tabHome"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tabCercano"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:orientation="vertical">
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tabRubro"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:orientation="vertical">
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tabBuscar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:orientation="vertical">
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

感谢您的时间。

此致。

4

0 回答 0