Cannot remove Padding from Tabs when using Custom views with Tab Layout

不想你离开。 提交于 2019-11-28 22:51:24
Kyrie Liu
 <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="210dp"
            android:layout_height="28dp"
            android:layout_centerInParent="true"
            android:background="@drawable/bg_forum_tab"
            app:tabIndicatorColor="@color/colorBtnBg"
            app:tabIndicatorHeight="0dp"
            app:tabPaddingBottom="-1dp"
            app:tabPaddingEnd="-1dp"
            app:tabPaddingStart="-1dp"
            app:tabPaddingTop="-1dp"
            app:tabSelectedTextColor="@color/colorBtnBg"
            app:tabTextColor="@color/colorWhite" />

Set tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom like this.

It worked for me:

In your TabLayout you have to set tabPaddingEnd and tabPaddingStart to 0dp.

In your custom view you have to set the layout_width and layout_height to match_parent in order to fill all space with your custom color.

The TabLayout:

<android.support.design.widget.TabLayout
    android:id="@+id/tl_dashboard"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentBottom="true"
    app:tabGravity="fill"
    app:tabIndicatorColor="@color/colorRed"
    app:tabMode="fixed"
    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"/>

And custom view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
     <!--Your widgets-->        
</RelativeLayout>

Through XML you can remove only the left and right paddings like that:

 app:tabPaddingEnd="0dp"
 app:tabPaddingStart="0dp"

Notice, that this way doesn`t work for TOP and BOTTOM paddings, but I find next solution:

When you use custom views as tab item, you need to set LayoutParams to the view and set paddings 0.

    for (int i = 0; i < tabLayout.getTabCount(); i++) {           
        View tabView = LayoutInflater.from(context)
              .inflate(LayoutInflater.from(this), R.layout.item_main_tabview, null, false);

        tabView.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tabView.setPadding(0, 0, 0, 0);
        tabLayout.getTabAt(i).setCustomView(tabViewBinding.getRoot());
    }

I solved this by setting the margin & padding of your custom view's parent to zero, when adding new tabs to tab layout.

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(0,0,0,0);

    TabLayout.Tab newTab = mTabsBottom.newTab().setCustomView(R.layout.view_custom_tab);
    setupTabParentLayout(newTab.getCustomView(), lp);
    ..
    ..
    ..
    private void setupTabParentLayout(View customView, LinearLayout.LayoutParams lp)
    {
        LinearLayout tabParent = (LinearLayout) customView.getParent();
        tabParent.setLayoutParams(lp);
        tabParent.setPadding(0,0,0,0);
    }

Trick here was to use LinearLayout.LayoutParams for custom view's parent.

I know this is an old question but this can help someone else. Padding start and padding end is not removing space between tabs. Solution is :

android:clipToPadding="true"

 <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/colorGrayBackground"
        app:tabGravity="fill"
        app:tabPaddingStart="0dp"
        app:tabPaddingEnd="0dp"
        app:tabContentStart="0dp"
        app:tabIndicatorHeight="0dp"
        android:clipToPadding="true"
        app:tabMode="fixed" />

Then you can set paddings to "0dp". Hope it helps someone.

        <android.support.design.widget.TabLayout
            android:id="@+id/tabview"
            app:layout_constraintTop_toBottomOf="@+id/ivpromo"
            android:layout_width="0dp"
            app:tabMode="scrollable"
            app:tabIndicatorHeight="0dp"
            app:tabContentStart="0dp"
            android:clipToPadding="true"
            app:tabMaxWidth="45dp"
            app:tabGravity="fill"
            app:tabPaddingStart="0dp"
            app:tabPaddingEnd="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:tabIndicatorColor="@android:color/transparent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/tvcode"
            android:layout_height="40dp"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!