-1

I implement TabLayout to similiar Google Play Music app.

  • Portrait (Google Play Music): enter image description here
  • Landscape (Google Play Music): enter image description here

This is my layout :

 <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.design.widget.TabLayout>

My code to handle TabLayout :

private void init() {
    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mTabLayout.addTab(mTabLayout.newTab().setText("Category 1"));
    mTabLayout.addTab(mTabLayout.newTab().setText("Category 2"));
    mTabLayout.addTab(mTabLayout.newTab().setText("Category 3"));
    mTabLayout.addTab(mTabLayout.newTab().setText("Category 4"));
    mTabLayout.addTab(mTabLayout.newTab().setText("Category 5"));
    mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}

But result is TabLayout doensn't have margin on the left :

  • Portrait (my example):: enter image description here
  • Landscape (my example): enter image description here

I try this article but the result is not as expect. How can i custom TabLayout like Google Play Music app as show above ?

Community
  • 1
  • 1
green.android
  • 731
  • 3
  • 7
  • 22

1 Answers1

4

You should use android:marginLeft to the TabLayout.

<android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp">

    </android.support.design.widget.TabLayout>

To have different margins for portrait and landscape orientations, use separate values at values/dimens.xml and values-land/dimens.xml

Update:

Using margin makes a space, even when the tab is scrolled. So use padding, with clipToPadding="false"

<android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:clipToPadding="false">

        </android.support.design.widget.TabLayout>
Bob
  • 13,447
  • 7
  • 35
  • 45