Android - 更改选项卡查看背景颜色问题

时间:2013-06-23 00:18:26

标签: android background-color tabview

我正在关注如何制作标签布局tutorial。我想改变标签的背景,所以我找到了如何做到这一点。问题是标签下面有一条线只在有选定项目时发生变化(如下图所示)。

有没有办法设置该行的颜色?

enter image description here

的活动:

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

感谢您的时间。

最好的问候。

0 个答案:

没有答案