Android 单击时如何更改选项卡的颜色? TabWidget tw=getTabWidget(); 对于(int i=0;i

Android 单击时如何更改选项卡的颜色? TabWidget tw=getTabWidget(); 对于(int i=0;i,android,android-animation,android-tabhost,Android,Android Animation,Android Tabhost,我的活动中有两个选项卡。此代码将第一个选项卡的颜色固定为“红色”,但在单击时不会使第二个选项卡更改为“红色”。 代码中应该做哪些更改 为您的任务使用xml bg选择器,这是做您想做的事情的更好方法 当然,如果您想在代码中尝试此代码 以settabColor(您的TabHost)传递选项卡主机 publicstaticvoidsettabcolor(TabHost-TabHost){ 对于(inti=0;i请在tabactivity类中尝试此代码。它可能对您有所帮助 public static v

我的活动中有两个选项卡。此代码将第一个选项卡的颜色固定为“红色”,但在单击时不会使第二个选项卡更改为“红色”。
代码中应该做哪些更改

为您的任务使用xml bg选择器,这是做您想做的事情的更好方法

当然,如果您想在代码中尝试此代码

以settabColor(您的TabHost)传递选项卡主机

publicstaticvoidsettabcolor(TabHost-TabHost){

对于(inti=0;i请在tabactivity类中尝试此代码。它可能对您有所帮助

public static void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
    {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
for(int i=0;i
但这不会设置整个页面的背景吗?首先要更改活动背景或选项卡背景。
public static void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
    {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
    for(int i = 0; i < getTabWidget().getChildCount(); i++)
    {
        if(getTabWidget().getChildAt(i).isSelected())
        {
            //selected tab
            getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
        }
        else
        {
            //un-selected tabs
            getTabWidget().getChildAt(i).setBackgroundColor(Color.GREEN);
        }
    }