Tablayout is a control in the new Android 5.0 feature--material design, which is a tabbed navigation bar that is often combined with Viewpager to complete the navigation of the page.
As with other MD controls, you need to declare dependencies in the Gradle file before using tablayout:
Compile ' com.android.support:design:25.0.0 '
1. Tablayout Properties:
App:tabIndicatorColor:TabLayout the color of the cue bar below app:tabIndicatorHeight:TabLayout The height app for the hint bar below : Tabselectedtextcolor:tablayout the text color of the selected tab in the text color app:tabTextColor:TabLayout without the tab selected
2, Tablayout and Viewpager combined use:
The most common usage of tablayout is to use the navigation of the completed page with Viewpager. It is important to note that Tablayout needs to be bound to Viewpager before the tab header is set, otherwise the tab title text disappears. The code is as follows:
//bind Tablayout and ViewpagerTabs.setupwithviewpager (VP); //tablayout in the tab arrangement, fixed for the split layout; scrollable is a linear arrangement, slidingTabs.settabmode (tablayout.mode_fixed); //add text to the tab in Tablayout for(inti = 0; I < titles.size (); i++) {Tabs.gettabat (i). SetText (Titles.get (i)); } //add an adapter for ViewpagerVp.setadapter (NewFragmentpageradapter (Getsupportfragmentmanager ()) {@Override PublicFragment GetItem (intposition) { returnFragments.get (position); } @Override Public intGetCount () {returnfragments.size (); } });
3. Tablayout Add Icon:
If it is the tablayout on the main page, then there must be an icon. We used to use Radiogroup to achieve this function, now with tablayout, it can be more easily implemented. The implementation method is to set the icon for each tab through the SetIcon () method. For example: The above code can be changed to:
//bind Tablayout and ViewpagerTabs.setupwithviewpager (VP); //tablayout in the tab arrangement, fixed for the split layout; scrollable is a linear arrangement, slidingTabs.settabmode (tablayout.mode_fixed); //add an icon and text to the tab in Tablayout for(inti = 0; I < titles.size (); i++) {Tabs.gettabat (i). SetText (Titles.get (i)). SetIcon (R.mipmap.ic_launcher); } //add an adapter for ViewpagerVp.setadapter (NewFragmentpageradapter (Getsupportfragmentmanager ()) {@Override PublicFragment GetItem (intposition) { returnFragments.get (position); } @Override Public intGetCount () {returnfragments.size (); } });
Operation Result:
The above is the basic usage of tablayout introduction, the following stickers out code in the cloud source, for your reference.
Demo Address
Use of the Tablayout "ANDROID-MD"