2017-09-06 141 views
-2

林创建一个Android应用程序的工作室,我想,当用户在标签按钮的标签图标点击/彩色更改选项卡布局选项卡颜色?

变化,这是我的代码:

 tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
      @Override 
      public void onTabChanged(String tabId) { 

       int id= tabHost.getCurrentTab(); 
       // TabLayout.setSelectedTabIndicatorColor(int color); // Error because of non static method 

//    if(id==0){ 
//     TabHost.TabSpec tab1spec = tabHost.newTabSpec("List"); 
//     tab1spec.setIndicator("", getResources().getDrawable(R.drawable.home2)); 
//    } 
//    if(id==1){ 
//     TabHost.TabSpec tab1spec = tabHost.newTabSpec("Config"); 
//     tab1spec.setIndicator("", getResources().getDrawable(R.drawable.lab2)); 
//    } 
//    if(id==2){ 
//     TabHost.TabSpec tab1spec = tabHost.newTabSpec("More"); 
//     tab1spec.setIndicator("", getResources().getDrawable(R.drawable.more2)); 
//    } 

      } 
     }); 

任何想法?

感谢

回答

1
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){ 
       @Override 
       public void onTabChanged(String tabId) { 
       int tab = tabHost.getCurrentTab(); 
       tabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.parseColor("#FFFFFF")); 
       setTabColor(tabHost); 
       } 
      });   
    } 

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

由于它工作得很好, – Disco4uf

相关问题