【问题标题】:Is it possible to change the color of selected Tab in android?是否可以在android中更改所选标签的颜色?
【发布时间】:2012-03-10 12:33:12
【问题描述】:

您好,我的标签小部件中有两个标签,我想为两个标签应用两种不同的颜色。我到处搜索,应用标签时几乎所有颜色都相同。

更新

first tab when selected red color

second tab when selected blue color

这是我的代码

tabHost = (TabHost)findViewById(android.R.id.tabhost);
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");//these are color red
    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");//these color blue
    firstTabSpec.setIndicator("Sales Info",getResources().getDrawable(R.drawable.sales));
    Intent photosIntent = new Intent(this, a.class);
    firstTabSpec.setContent(photosIntent);
    secondTabSpec.setIndicator("Service Info",getResources().getDrawable(R.drawable.services));
    Intent photosIntent1 = new Intent(this, b.class);
    secondTabSpec.setContent(photosIntent1);
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);

【问题讨论】:

    标签: android colors tabs


    【解决方案1】:

    试试这个:

    ...onCreate(){
    
         ...
         tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    
        @Override
        public void onTabChanged(String arg0) {
    
            setTabColor(tabHost);
        }
         });
         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_CYAN); //unselected
    
        if(tabhost.getCurrentTab()==0)
               tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_RED); //1st tab selected
        else
               tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_BLUE); //2nd tab selected
    }
    

    【讨论】:

    • 感谢 hiral 它的工作,但我应用颜色代码 Color.green(0xCFEB5D) 而不是 (color.GREEN)。它为什么不工作?
    • 它应该可以工作,因为 Color.GREEN 可以直接访问。然后您可以使用 Color.parseColor("#color_code_of_green");
    【解决方案2】:

    您可以使用setOnTabChangedListener 为您的TabHost 设置Listener 并动态更改它,

      public void onCreate(Bundle savedInstanceState){
       // add your tabs here
    
       // set the First Tab as selected Tab.
      setSelectedTabColor();
    }
    

    创建一个方法来设置TabSelectedUnselected 颜色。

     private void setSelectedTabColor() {
            for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)  
            {  
                tabHost.getTabWidget().getChildAt(i)
                                                .setBackgroundColor(Color.WHITE);  
            }  
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                                                  .setBackgroundColor(Color.RED); 
        }
    

    然后在您的onTabChanged() 中,您可以动态更改背景。

    @Override  
        public void onTabChanged(String tabId) {  
            setSelectedTabColor(); 
        } 
    

    selectedunselected 标签可以使用相同的名称,here 是相同的博客。

    【讨论】:

    • 谢谢,类型不匹配:无法从 void 转换为 View 出现错误 View view = myTabHost.getTabWidget().getChildAt(tab) .setBackgroundColor(Color.CYAN);这一行
    • 未选中的标签也是青色
    • @micro 我已经添加了相同的链接,您如何也可以管理未选择的标签。
    • @thank you lalit 它的工作,但我应用颜色代码 Color.green(0xCFEB5D) 而不是 (color.GREEN)。它为什么不工作?
    【解决方案3】:

    使用 setIndicator(View view) 代替 setIndicator(CharSequence label, Drawable icon)。您将传递的视图的背景设置(例如,如果您正在扩展父布局的 xml)应该是处理点击的 ColorStateList。

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 2019-07-14
      • 1970-01-01
      • 2014-06-03
      • 2016-04-28
      • 2019-07-15
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多