Этот код для 2 вкладок в tablayout:
Один выбран, а два - невыбранный режим
Drawable XML:
При выборе левой вкладки:
tabselectionleft:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"/>
<solid android:color="@android:color/white"/>
</shape>
notabselectionleft:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="@color/colorAccent1" android:endColor="@color/colorAccent3"/>
<corners android:topRightRadius="5dp"
android:bottomRightRadius="5dp" />
</shape>
</item>
</layer-list>
При выборе правой вкладки:
tabselectionright:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomRightRadius="5dp"
android:topRightRadius="5dp"/>
<solid android:color="@android:color/white"/>
</shape>
notabselectionright:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="true">
<corners
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp" />
<gradient
android:endColor="@color/colorAccent3"
android:startColor="@color/colorAccent1" />
</shape>
</item>
В основной вкладке раскладки выписка
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabMode="fixed"
app:tabGravity="fill"
android:fitsSystemWindows="true"
android:clickable="true"
app:tabIndicatorColor="@color/colorAccent2"
app:tabIndicatorHeight="3dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:tabPaddingStart="10dp"
app:tabPaddingEnd="10dp"
android:clipToPadding="true"
android:clipChildren="true"
/>
Вызов этого Drawable должен происходить программно, как показано ниже:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
firstTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(0);
secondTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1);
tabLayout.setOnTabSelectedListener(this);
firstTab.setBackground(getDrawable(R.drawable.tabselectionleft));
secondTab.setBackground(getDrawable(R.drawable.notabselectionleft));
}
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
int selectedTabPosition = tab.getPosition();
if (selectedTabPosition == 0)
{ // that means first tab
firstTab.setBackground(getDrawable(R.drawable.tabselectionleft));
secondTab.setBackground(getDrawable(R.drawable.notabselectionleft));
} else if (selectedTabPosition == 1)
{ // that means it's a last tab
firstTab.setBackground(getDrawable(R.drawable.notabselectionright));
secondTab.setBackground(getDrawable(R.drawable.tabselectionright));
}