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

前端 未结 8 901
傲寒
傲寒 2020-12-15 04:18

I have added a Relative Layout in Custom View and have added this in Tab Layout. I am using a white background for tabs and have not applied any padding in tabs custom layou

相关标签:
8条回答
  • 2020-12-15 04:31

    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.

    0 讨论(0)
  • 2020-12-15 04:34

    I create TabLayout from code and me help only this

    for (int position = 0; position < mTabLayout.getTabCount(); position++) {
        TabLayout.Tab tab = mTabLayout.getTabAt(position);
        if (tab != null) {
            ViewCompat.setPaddingRelative(tab.view, 0, 0, 0, 0); 
        }
    }
    
    0 讨论(0)
  • 2020-12-15 04:38

    In case you have tried many things and none of them did work, here is what I ended up doing: just setting

    android:layout_height="20dp

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <androidx.viewpager.widget.ViewPager
            android:layout_width="match_parent"
            android:layout_gravity="bottom"
            android:layout_height="130dp"/>
    
        <com.google.android.material.tabs.TabLayout
            android:layout_height="20dp"
            android:layout_width="match_parent"
            android:layout_gravity="bottom"
            app:tabBackground="@drawable/tab_selector"
            app:tabGravity="center"
            app:tabIndicatorHeight="0dp"/>
    
    </FrameLayout>
    
    0 讨论(0)
  • 2020-12-15 04:39
    <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"/>
    
    0 讨论(0)
  • 2020-12-15 04:40

    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>
    
    0 讨论(0)
  • 2020-12-15 04:41

    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.

    0 讨论(0)
提交回复
热议问题