【问题标题】:Change tab layout tab color?更改选项卡布局选项卡颜色?
【发布时间】:2017-09-06 08:20:09
【问题描述】:

我正在创建一个 android studio 应用程序,当用户单击选项卡按钮时,我想要选项卡图标/颜色

更改这是我的代码:

       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));
//                }

            }
        });

有什么想法吗?

谢谢

【问题讨论】:

    标签: java android android-tabhost


    【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多