How to set Tab View Indicator background color in Android

后端 未结 4 1966
我在风中等你
我在风中等你 2021-01-06 05:41

How to Change color on click tab Host. i can change image but color are not change.

public class MainActivity extends Activity {
    @Override
    protected          


        
相关标签:
4条回答
  • 2021-01-06 05:57

    //set background colors for tabs

       for (int i = 1; i <= 3; i++) {
        tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundColor(getResources().getColor(R.color.gradient));
    
           }
    
    0 讨论(0)
  • 2021-01-06 06:00

    For that your should use the event onTabChangedlistener and inside put any of the loops that you want as in the picked answer.

    tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                setTabColor(tabhost); 
            }
        });
    

    You can read at this article, this will help you but it's in spanish.

    http://www.johnarij.ml/2017/04/19/cambio-de-color-de-tab-en-tabhost-android/

    0 讨论(0)
  • 2021-01-06 06:19

    You can use this function just pass your tabHost in it

    public static void setTabColor(TabHost tabhost) {
            for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
            {
                tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white); //unselected
            }
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundResource(R.color.tab_selected); // selected
        }
    
    0 讨论(0)
  • 2021-01-06 06:24
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
            tabHost.setup();
    
            TabSpec spec1=tabHost.newTabSpec("Tab 1");
            spec1.setContent(R.id.tab1);`enter code here`
            spec1.setIndicator("Tab 1", getResources().getDrawable(setBackgroundColor(view.Color.RED));
    
            TabSpec spec2=tabHost.newTabSpec("Tab 2");
            spec2.setIndicator("Tab 2", getResources().getDrawable(setBackgroundColor(view.Color.GREAN));
            spec2.setContent(R.id.tab2);
    
            TabSpec spec3=tabHost.newTabSpec("Tab 3");
            spec3.setIndicator("Tab 3", getResources().getDrawable(setBackgroundColor(view.Color.BLACK));
            spec3.setContent(R.id.tab3);
    
            tabHost.addTab(spec1);
            tabHost.addTab(spec2);
            tabHost.addTab(spec3);
        }
    }
    
    0 讨论(0)
提交回复
热议问题