TabLayout not filling width when tabMode set to 'scrollable'

前端 未结 17 774
傲寒
傲寒 2020-12-02 16:58

I have added TabLayout (from support library v22.2.1) to my Fragment as:



        
17条回答
  •  隐瞒了意图╮
    2020-12-02 17:22

    I was struggling as well and most options above did not work for me, so I created a version on the answer of DTX12.

    I have a boolean to check for tablet or not and make a check on landscape and portrait. Then set the layout based on amount of tabs.

    You might need to make the method public and call it on rotation / configurationchange.

    In the CustomTabLayout Code i replaced the mintabswidth method

    private void initTabMinWidth()
    {
    
    
    
        boolean isTablet = getContext().getResources().getBoolean(R.bool.device_is_tablet);
        boolean isLandscape = (wh[WIDTH_INDEX] > wh[HEIGHT_INDEX]) ? true : false;
    
    
        if (isTablet)
        {
            if (isLandscape)
            {
                if (this.getTabCount() > 8)
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_SCROLLABLE);
                } else
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_FIXED);
                }
            } else
            {
                if (this.getTabCount() > 6)
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_SCROLLABLE);
                } else
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_FIXED);
                }
            }
        } else
        {
            if (isLandscape)
            {
                if (this.getTabCount() > 6)
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_SCROLLABLE);
                } else
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_FIXED);
                }
            } else
            {
                if (this.getTabCount() > 4)
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_SCROLLABLE);
                } else
                {
                    this.setTabGravity(TabLayout.GRAVITY_FILL);
                    this.setTabMode(TabLayout.MODE_FIXED);
                }
            }
        }
    
    
    }
    

提交回复
热议问题