如何在TabHost中改变颜色[英] How to change colour in TabHost

本文是小编为大家收集整理的关于如何在TabHost中改变颜色的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用TabHost Formate进行应用程序,选项卡以默认颜色显示,是否有可能将默认颜色更改为我们自己的颜色.?我从谷歌那里得到了一些想法,

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub
         for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
               tabHost.getTabWidget().getChildAt(i).setBackgroundColor(R.color.transparent); //unselected
            }
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#000011")); // selected
    }

每当我这样做时,我都会强行关闭错误.如果有人知道改变背景颜色,请指导我.

推荐答案

您需要先更改默认出现

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
    if (i == 0) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));

    else tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
    }    

在处理onTabChanged事件之前

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
           tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parsecolor("#7392B5")); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected
}
});

本文地址:https://www.itbaoku.cn/post/102418.html

问题描述

I am doing application with TabHost formate, The tab is displaying with Default color, is there any possibility to change default color to our own color.? I got some ideas from google,

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub
         for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
               tabHost.getTabWidget().getChildAt(i).setBackgroundColor(R.color.transparent); //unselected
            }
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#000011")); // selected
    }

whenever i doing this i am getting force close error. if anyone having idea of changing background color, pls guide me.

推荐答案

You need first to change the default appearence

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
    if (i == 0) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));

    else tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
    }    

before dealing with the onTabChanged event

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
           tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parsecolor("#7392B5")); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected
}
});