Use of Android Material design-tablayout

Source: Internet
Author: User

Tablayout is located in Android.support.design.widget.TabLayout.

Generally used in conjunction with Viewpager. Previously open Source Library Viewpagerindicator can also be implemented, but Tablayout is officially provided.

The following uses Viewpager + Tablayout to achieve the effect of clicking the tab switch page. The Viewpager used in the TextView to display a word, you can change the TextView to Fragment and other things to achieve more complex.

Layout file Activity_main.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayout3     xmlns:android= "Http://schemas.android.com/apk/res/android"4 Xmlns:app= "Http://schemas.android.com/apk/res-auto"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent">7   <Android.support.v7.widget.Toolbar8       Android:id= "@+id/toolbar"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:background= "@color/colorprimary" A Android:minheight= "? Attr/actionbarsize" - Android:theme= "@style/themeoverlay.appcompat.actionbar" - App:popuptheme= "@style/themeoverlay.appcompat.light"/> the   <Android.support.design.widget.TabLayout -       Android:id= "@+id/tablayout" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:layout_below= "@+id/toolbar"/> -   <Android.support.v4.view.ViewPager +       Android:id= "@+id/viewpager" A Android:layout_width= "Match_parent" at Android:layout_height= "Match_parent" - Android:layout_below= "@+id/tablayout"/> - </Relativelayout>

Viewpageradapter.java

1 ImportAndroid.content.Context;2 ImportAndroid.graphics.Color;3 ImportAndroid.support.v4.view.PagerAdapter;4 Importandroid.view.Gravity;5 ImportAndroid.view.View;6 ImportAndroid.view.ViewGroup;7 ImportAndroid.widget.TextView;8 9  Public classViewpageradapterextendsPageradapter {Ten   Private Static FinalString TAG = "Viewpageradapter"; One   PrivateString[] Tabtitles = {"All", "video", "Sound", "Picture", "Satin", "ad", "Plot" }; A   PrivateContext Mcontext; -  -    PublicViewpageradapter (Context context) { theMcontext =context; -   } -  -@Override Public intGetCount () { +     returntabtitles.length; -   } +  A@Override Public Booleanisviewfromobject (View view, Object object) { at     returnView = =object; -   } -  -@Override PublicObject Instantiateitem (ViewGroup container,intposition) { -TextView view =NewTextView (mcontext); - View.settext (tabtitles[position]); in View.settextcolor (color.black); -View.settextsize (20); to view.setgravity (gravity.center); + Container.addview (view); -     returnview; the   } *  $@Override Public voidDestroyitem (ViewGroup container,intposition, Object object) {Panax Notoginseng Container.removeview ((TextView) object); -   } the  +@Override PublicCharsequence Getpagetitle (intposition) { A     returnTabtitles[position]; the   } +}

Mainactivity.java

1  Public classMainactivityextendsappcompatactivity {2 3@Overrideprotected voidonCreate (Bundle savedinstancestate) {4     Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.activity_main);6Tablayout tablayout =(tablayout) Findviewbyid (r.id.tablayout);7Viewpager Viewpager =(Viewpager) Findviewbyid (R.id.viewpager);8Viewpageradapter adapter =NewViewpageradapter (mainactivity. This);9 Viewpager.setadapter (adapter);Ten Tablayout.setupwithviewpager (Viewpager); One   } A}

Realize:

Tablayout style
App:tabSelectedTextColor:Tab the color of the selected font
App:tabTextColor:Tab Color of unselected fonts
Color of the APP:TABINDICATORCOLOR:TAB indicator subscript

app:tabindicatorheight= "2DP"
App:tabpaddingstart= "12DP"
App:tabpaddingend= "12DP"
app:tabpaddingtop= "12DP"
App:tabpaddingbottom= "12DP"
App:tabpadding= "12DP"
App:tabbackground= "@android: Color/darker_gray"
App:tabtextappearance= ""

App:tabmode: Optional scrollable and fixed, default to fixed. Scrollable for multiple tab cases

When the number of tabs is not sufficient to fill the full screen, and when the Tabmode is scrollable, the following occurs: Just set the tabmode to fixed or remove this property, because the default is fixed.

Reference: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0731/3247.html

Use of Android Material design-tablayout

Related Article

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.