Use of the Tablayout "ANDROID-MD"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.