是否有可能在android中更改所选Tab的颜色?

Mer*_*rcy 8 tabs android colors

嗨我在我的标签小部件中有两个标签,我想为两个标签应用两种不同的颜色.在任何地方搜索,大多数应用标签时所有颜色都相同.

更新

选择红色时的第一个选项卡

选择蓝色时的第二个选项卡

在这里我的代码

tabHost = (TabHost)findViewById(android.R.id.tabhost);
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");//these are color red
    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");//these color blue
    firstTabSpec.setIndicator("Sales Info",getResources().getDrawable(R.drawable.sales));
    Intent photosIntent = new Intent(this, a.class);
    firstTabSpec.setContent(photosIntent);
    secondTabSpec.setIndicator("Service Info",getResources().getDrawable(R.drawable.services));
    Intent photosIntent1 = new Intent(this, b.class);
    secondTabSpec.setContent(photosIntent1);
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
Run Code Online (Sandbox Code Playgroud)

Hir*_*ral 13

试试这个:

...onCreate(){

     ...
     tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String arg0) {

        setTabColor(tabHost);
    }
     });
     setTabColor(tabHost);
...
}

//Change The Backgournd Color of Tabs
public void setTabColor(TabHost tabhost) {

    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(COLOR_CYAN); //unselected

    if(tabhost.getCurrentTab()==0)
           tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_RED); //1st tab selected
    else
           tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_BLUE); //2nd tab selected
}
Run Code Online (Sandbox Code Playgroud)


Lal*_*ani 7

您可以设置ListenerTabHost使用setOnTabChangedListener并动态更改它,

  public void onCreate(Bundle savedInstanceState){
   // add your tabs here

   // set the First Tab as selected Tab.
  setSelectedTabColor();
}
Run Code Online (Sandbox Code Playgroud)

创建一个将设置SelectedUnselected颜色的方法Tab.

 private void setSelectedTabColor() {
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)  
        {  
            tabHost.getTabWidget().getChildAt(i)
                                            .setBackgroundColor(Color.WHITE);  
        }  
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                                              .setBackgroundColor(Color.RED); 
    }
Run Code Online (Sandbox Code Playgroud)

然后onTabChanged()你可以动态改变背景.

@Override  
    public void onTabChanged(String tabId) {  
        setSelectedTabColor(); 
    } 
Run Code Online (Sandbox Code Playgroud)

你可以使用相同的selectedunselectedTab,here是相同的博客.