Android更改标签的背景

Android更改标签的背景,android,android-tabhost,Android,Android Tabhost,有人知道我如何编辑标签背景,使我有一个红色渐变背景时,它没有被选中,当它被选中一个深红色渐变?还要将文本颜色更改为白色?在drawables文件夹中创建一个新文件,例如background.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use

有人知道我如何编辑标签背景,使我有一个红色渐变背景时,它没有被选中,当它被选中一个深红色渐变?还要将文本颜色更改为白色?

在drawables文件夹中创建一个新文件,例如background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use red-->
    <item android:drawable="@drawable/gradient_red"
          android:state_selected="true"/>
    <!-- When not selected, use dark rebg-->
    <item android:drawable="@drawable/gradien_dark_red"/>
</selector>

使用新的绘图工具作为背景。

您可以使用此代码

TabHost.TabSpec spec;
TabHost tabHost = getTabHost();
spec = tabHost.newTabSpec("1").setIndicator("Tab Host 1", res.getDrawable(R.drawable.XXX)).setContent(intent_name);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
setTabColor(tabHost);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
          setTabColor(tabHost);
    }
});
}

public static void setTabColor(TabHost tabhost) {
    for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000")); // unselected
    }

    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#74df00")); // selected
}
TabHost.TabSpec;
TabHost TabHost=getTabHost();
spec=tabHost.newTabSpec(“1”).setIndicator(“Tab Host 1”,res.getDrawable(R.drawable.XXX)).setContent(intent_name);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
setTabColor(tabHost);
tabHost.setOnTabChangedListener(新的ontabchangedListener(){
@凌驾
已更改的公共无效项(字符串选项卡ID){
setTabColor(tabHost);
}
});
}
公共静态无效setTabColor(TabHost TabHost){
对于(int i=0;i
您可以为此自定义绘图谢谢,我在这里得到一个空指针异常。setBackgroundColor(Color.parseColor(“#74df00”);尝试设置不同的颜色代码,因为我使用了相同的代码,并且运行良好。