Android Tab Layout not taking up full width with custom view

前端 未结 8 2427
谎友^
谎友^ 2021-02-15 17:03

Android TabLayout tabPaddingTop and tabPaddingBottom not being removed

Please refer to the above issue as well.

Even since i updated my design library to \"23.2.

相关标签:
8条回答
  • 2021-02-15 17:39

    I was stuck for hours until I found the method tab.setCustomView(idRes). Changing from inflated to this worked for me. Alernatively, if you are inflating you can use TabLayout.TabView as root viewgroup.

    Apart from this, I am not sure if it is helpful but I used minWidth to the custom view layout.

    This is what I wanted

    0 讨论(0)
  • 2021-02-15 17:43

    In my case the problem was giving fixed height to the custom layout that i use for tabs. Using match_parent fixed my issue.

    0 讨论(0)
  • 2021-02-15 17:44

    Just pass your tabLayout to this method and it's done!

    private void setTabLayoutMatchParent(TabLayout tabLayout) {
        final ViewGroup tabLayoutChild = (ViewGroup)(tabLayout.getChildAt(0));
        int tabLen = tabLayoutChild.getChildCount();
    
        for (int j = 0; j < tabLen; j++) {
            View v = tabLayoutChild.getChildAt(j);
            v.setPadding(0, 0, 0, 0);
        }
    }
    
    0 讨论(0)
  • 2021-02-15 17:48

    Instead of dealing with TabLayout subviews, you can access TabLayout.Tab view directly and set the padding to 0 just like the code below.

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        View tabView = tabLayout.getTabAt(i).view;
        tabView.setPadding(0, 0, 0, 0);
    }
    

    I'm doing this in the onCreateView callback and it works perfectly.

    0 讨论(0)
  • 2021-02-15 17:54

    Try adding below attributes to TabLayout:

    app:tabPaddingStart="-1dp"
    app:tabPaddingEnd="-1dp"
    

    Hope it'll work.

    0 讨论(0)
  • 2021-02-15 17:57

    Setting different values or layout params did not work, so the only solution I got was to add the following, after you add the tabs to your tab layout,

    final ViewGroup test = (ViewGroup)(tabs.getChildAt(0));//tabs is your Tablayout
    int tabLen = test.getChildCount();
    
    for (int i = 0; i < tabLen; i++) {
                View v = test.getChildAt(i);
                v.setPadding(0, 0, 0, 0);
            }
    
    0 讨论(0)
提交回复
热议问题