是否可以在android中更改所选选项卡的颜色?

是否可以在android中更改所选选项卡的颜色?,android,colors,tabs,Android,Colors,Tabs,嗨,我的标签小部件中有两个标签,我想为两个标签应用两种不同的颜色。我到处搜索,大多数情况下,应用标签时所有颜色都是相同的 更新 选择红色时的第一个选项卡 第二个选项卡选择蓝色时 这是我的密码 tabHost = (TabHost)findViewById(android.R.id.tabhost); TabSpec firstTabSpec = tabHost.newTabSpec("tid1");//these are color red TabSpec secondTabSp

嗨,我的标签小部件中有两个标签,我想为两个标签应用两种不同的颜色。我到处搜索,大多数情况下,应用标签时所有颜色都是相同的

更新

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

第二个选项卡选择蓝色时

这是我的密码

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

您可以使用
setOnTabChangedListener
为您的
TabHost
设置
Listener
,并动态更改它

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

   // set the First Tab as selected Tab.
  setSelectedTabColor();
}
创建一种方法,用于设置
选项卡
选定
未选定
颜色

 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); 
    }
您可以对选中的
和未选中的
选项卡使用相同的设置,这是相同的博客。

尝试以下操作:

...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
}
…onCreate(){
...
tabHost.setOnTabChangedListener(新的ontabchangedListener(){
@凌驾
已更改的公共void onTabChanged(字符串arg0){
setTabColor(tabHost);
}
});
setTabColor(tabHost);
...
}
//更改选项卡的背景颜色
公共void setTabColor(TabHost TabHost){

对于(int i=0;i使用setIndicator(视图视图)而不是setIndicator(字符序列标签,可绘制图标)。您将传递的视图的背景设置(例如,如果正在膨胀xml,则显示父布局)应该是一个ColorStateList来处理点击。

谢谢,类型不匹配:无法从void转换为View am get error View View=myTabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.CYAN);此行未选中的选项卡也为青色color@micro我已经添加了相同的链接,您如何管理未选中的选项卡。@谢谢您,我使用了颜色代码color.green(0xCFEB5D)而不是(color.green)。它不工作为什么?让我们感谢您,我使用了颜色代码color.green(0xCFEB5D)而不是(color.GREEN)。它不起作用为什么?它应该起作用,因为color.GREEN可以直接访问。然后可以使用color.parseColor(“color\u code\u of\u GREEN”);
...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
}