简体   繁体   English

在 TabActivity 中更改选项卡背景颜色

[英]change tab background color in TabActivity

I am trying to change the background color of tab in TabActivity .我正在尝试更改TabActivity中选项卡的背景颜色。 for that I did like below,为此,我确实喜欢下面,

tabHost.getTabWidget().getChildAt(totalTabs1-1).setBackgroundColor(Color.parseColor("#984b9d"));

but its not working properly what i want.但它不能正常工作我想要的。

Is there any other way to do it?有没有其他方法可以做到?

Thank You谢谢你

For this u have to write a xml file for tab selector inside drawable folder.为此,您必须在 drawable 文件夹中为选项卡选择器编写一个 xml 文件。

tab_selector.xml tab_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/tab_selectinfo"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/tab_unselectinfo" />
</selector>

and at the time of initialization of tab just do like below,并在选项卡初始化时执行如下操作,

tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.tab_selector)).setContent(intent);

You can try this:你可以试试这个:

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

    @Override
    public void onTabChanged(String arg0) {

        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.DKGRAY); //unselecte
     tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.LTGRAY); // selected
}

use following使用以下

for (int i = 0; i < Global.host.getTabWidget().getChildCount(); i++) {
            Global.host.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.inactbg));
            TextView tv = (TextView) Global.host.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#ffffff"));

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM