Rounded corners for TABS in android

后端 未结 7 2142
甜味超标
甜味超标 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:52

    This code are for 2 tabs in tablayout:

    One is selected and two is unselected mode

    Drawable xml:

    When selecting Left tab:

    tabselectionleft:

    
    
                
    
        
    
    
    

    notabselectionleft:

    
    
    
        
    
            
                
    
                
            
        
    
    
    
    

    When selecting right tab:

    tabselectionright:

    
    
        
        
    
    
    

    notabselectionright:

    
    
        
    
            
    
    
                
                
    
    
            
    
    
        
    

    In main layout tablayout decelartion

    
    

    Calling this drawable should happen programmatically as shown below:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_simple_tabs);
     viewPager = (ViewPager) findViewById(R.id.viewpager);
            setupViewPager(viewPager);
    
            tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
            firstTab = ((ViewGroup)    tabLayout.getChildAt(0)).getChildAt(0);
            secondTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1);
            tabLayout.setOnTabSelectedListener(this);
    
            firstTab.setBackground(getDrawable(R.drawable.tabselectionleft));
    
            secondTab.setBackground(getDrawable(R.drawable.notabselectionleft));
    
    
    
        }
    public void onTabSelected(TabLayout.Tab tab) {
    
            viewPager.setCurrentItem(tab.getPosition());
    
            int selectedTabPosition = tab.getPosition();
    
            if (selectedTabPosition == 0)
            { // that means first tab
                firstTab.setBackground(getDrawable(R.drawable.tabselectionleft));
                      secondTab.setBackground(getDrawable(R.drawable.notabselectionleft));
    
    
            } else if (selectedTabPosition == 1)
            { // that means it's a last tab
    
                firstTab.setBackground(getDrawable(R.drawable.notabselectionright));
                secondTab.setBackground(getDrawable(R.drawable.tabselectionright));
    
    
            }
    

提交回复
热议问题