Is it possible to change actionbar tab indicator programmatically

后端 未结 2 1994
感动是毒
感动是毒 2020-11-30 03:47

How can i change programmatically the selected tab indicator of my action bar ? i have read about tab styling, and Tab.setCustomView() method, but none of these helps :

2条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 04:05

    I have succeeded to implement what i wanted by using @Padma's answer to generate my tab indicator backgrounds : i needed 5 selectors : green, yellow, blue, orange and red. So i created 5 xml drawables (tabs_selector_red.xml, tabs_selector_blue.xml, etc...) :

    tabs_selector_green.xml :

        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    I also created a layer-list for each xml background : layer_bg_selected_tabs_green.xml

    
        
            
    
            
        
    
    
        
            
        
    
    

    And finally, in the Java, i switch the background dynamically buy using selected tab's custom view and index :

    private static final int[] TABS_BACKGROUND = {
            R.drawable.tabs_selector_orange, R.drawable.tabs_selector_green,
            R.drawable.tabs_selector_red, R.drawable.tabs_selector_blue,
            R.drawable.tabs_selector_yellow };
    /*
    BLA BLA BLA
    */
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
        tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
        tab.setCustomView(tabLayout);
    /* ... */
    }
    

    Now let's add some screenshots :

    green blue red

提交回复
热议问题