TabLayout Tab Style

I am using the new TabLayout library from com.android.support:design . I want to change the background of selected / unselected tabs. I look at the sources and found only the tabBackground attribute, which changes the color of all tabs and does not control the selected color of the scoreboard.

How can I control the selected / unselected background of the tab?

+39
android android-design-library
Jun 10 '15 at 10:52
source share
4 answers

Define:

  <style name="AppTabLayout" parent="Widget.Design.TabLayout"> <item name="tabMaxWidth">@dimen/tab_max_width</item> <item name="tabIndicatorColor">?attr/colorAccent</item> <item name="tabIndicatorHeight">4dp</item> <item name="tabPaddingStart">6dp</item> <item name="tabPaddingEnd">6dp</item> <item name="tabBackground">?attr/selectableItemBackground</item> <item name="tabTextAppearance">@style/AppTabTextAppearance</item> <item name="tabSelectedTextColor">@color/range</item> </style> <!-- for text --> <style name="AppTabTextAppearance" parent="TextAppearance.Design.Tab"> <item name="android:textSize">12sp</item> <item name="android:textColor">@color/orange</item> <item name="textAllCaps">false</item> </style> 

To apply

 <android.support.design.widget.TabLayout style="@style/AppTabLayout" app:tabTextAppearance="@style/AppTabTextAppearance" android:layout_width="match_parent" .... /> 
+90
Jul 16 '15 at
source share

If you look at TabLayout.class , you will see the internal TabView.class for the actual tab layout. The same layout as any other with the isSelected attribute. Selecting a tab will also affect this, so all you have to do is create a background selection for the selection, for example

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/tab_bg_selected"/> <item android:drawable="@color/tab_bg_unselected"/></selector> 

and attach it to the tabBackground attribute, for example. in XML like

 <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:tabBackground="@drawable/tab_bg" app:tabIndicatorHeight="4dp"/> 
+10
Sep 03 '15 at 11:48
source share

I read How to create an ActionBar, the background of a tab on a selected tab and figure out what to do. This is a really similar problem, but I found an amazing solution specifically for TabLayout :

 <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/tab_layout_color" app:tabIndicatorHeight="48dp" app:tabIndicatorColor="@color/selected_tab_color" /> 

note that layout_height and tabIndicatorHeight are the same height. This way you get a pretty transient animation this way.

+7
Jun 10 '15 at 11:44
source share

I also met this problem. I just searched for tabIndicatorColor in the whole project and found the following code in some R.java :

  @see #TabLayout_tabBackground @see #TabLayout_tabContentStart @see #TabLayout_tabGravity @see #TabLayout_tabIndicatorColor @see #TabLayout_tabIndicatorHeight @see #TabLayout_tabMaxWidth @see #TabLayout_tabMinWidth @see #TabLayout_tabMode @see #TabLayout_tabPadding @see #TabLayout_tabPaddingBottom @see #TabLayout_tabPaddingEnd @see #TabLayout_tabPaddingStart @see #TabLayout_tabPaddingTop @see #TabLayout_tabSelectedTextColor @see #TabLayout_tabTextAppearance @see #TabLayout_tabTextColor 

So the problem is resolved. May this help you.
that is, I use IDEA

-9
Aug 10 '15 at 9:04
source share



All Articles