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

发布于 2024-12-23 06:57:13 字数 240 浏览 3 评论 0原文

我正在尝试更改 TabActivity 中选项卡的背景颜色。 为此,我确实喜欢下面的内容,

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

但它不能正常工作,如我想要的。

还有其他方法吗?

谢谢

I am trying to change the background color of tab in 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-12-30 06:57:13

为此,您必须在可绘制文件夹内为选项卡选择器编写一个 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>

并在选项卡初始化时执行如下操作,

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

For this u have to write a xml file for tab selector inside drawable folder.

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);
不即不离 2024-12-30 06:57:13

你可以试试这个:

...
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
}

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
}
画中仙 2024-12-30 06:57:13

使用以下

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

        }

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

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文