How to change Viewpager tab colour dynamically?

前端 未结 3 653
猫巷女王i
猫巷女王i 2021-01-20 18:26

How to change colour of Tabs like this? When I click/swipe to green or any other tab, the tab colour should change to that appropriate colour and rest of other tabs colour

3条回答
  •  自闭症患者
    2021-01-20 18:55

    There is tutorial Styling tabs in the Android action bar. You can choose your parent theme as Theme.Holo for API>=3, or Theme.AppCompat for support library V7, etc.

    And besides, for , you could set it to a selector you create for tab state change:

    android:background="@drawable/selector_tab"
    

    For selector_tab can be like:

    
    
        
         
    
        
    
    


    [UPDATE]

    For change tab color dynamically, suggest to use custom view with tab:

    //your_custom_tab.xml
    
    
    
    
      
    
    
    
    LinearLayout customView = (LinearLayout) getLayoutInflater().inflate(R.layout.your_custom_tab, null);
    

    then setCustomeView(customView) when add tab to ActionBar. And in your tab/page change listener:

    Tab selectedTab = yourActionBar.getSelectedTab();
    View tabView = selectedTab.getCustomView();
    tabView.setBackgroundColor(your_select_color);  
    


    To remove possible gap around tab caused by custom view, you can set tab style:

    
    

    and use your theme parent accordingly.

    Hope this help!

提交回复
热议问题