Rounded corners for TABS in android

后端 未结 7 2109
甜味超标
甜味超标 2020-12-21 05:35

I want to achieve rounded corners for the tab that I\'ve constructed in my application. So far I was able to come up with this

7条回答
  •  春和景丽
    2020-12-21 05:55

    Created alternate shapes to solve the problem

    Note: These xml files are in addition to the two mentioned.

    SELECTED TAB ALTERNATE.XML

    
    
    
    
    
    
    
    

    UNSELECTED TAB ALTERNATE.XML

    
    
    
    
     
    
    
    

    Afterwards, add this in your tab selection listener.

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
    
                viewPager.setCurrentItem(tab.getPosition());
    
                int selectedTabPosition = tab.getPosition();
    
    
                View firstTab = ((ViewGroup)    tabLayout.getChildAt(0)).getChildAt(0);
                View secondTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1);
    
    
                if (selectedTabPosition == 0)
                { // that means first tab
                    firstTab.setBackground(getDrawable(R.drawable.selected_tab));
                    secondTab.setBackground(getDrawable(R.drawable.unselected_tab));
    
    
                } else if (selectedTabPosition == 1)
                { // that means it's a last tab
    
                    firstTab.setBackground(getDrawable(R.drawable.selected_tab_alternate));
                    secondTab.setBackground(getDrawable(R.drawable.unselected_tab_alternate));
    
    
                }
            }
    

提交回复
热议问题