Android Indicator Tablatyout

Source: Internet
Author: User

<android.support.design.widget.TabLayout/>
New controls in the Android material design



Package com.weavey.loadinglayout;

Import Android.os.Bundle;
Import Android.support.design.widget.TabLayout;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;
Import Android.support.v4.content.ContextCompat;
Import Android.support.v4.view.ViewCompat;
Import Android.support.v4.view.ViewPager;

Import Com.weavey.fragment.TabContentFragment1;

Import java.util.ArrayList;

/**
* Android tablayout a sliding tab page
*/

public class Tablayoutactivity extends Fragmentactivity {
Private Tablayout Mtabtl;
Private Viewpager MCONTENTVP;
Private arraylist<string> tabindicators;
Private arraylist<fragment> tabfragments;
Private Contentpageradapter Contentadapter;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.layout_tablyout);
Setview ();
Setlistener ();
Set content
SetContent ();
Set Tab tab
Settab ();
}

private void Settab () {
Mtabtl.settabmode (tablayout.mode_fixed);
Mtabtl.settabtextcolors (Contextcompat.getcolor (this, Android. R.color.holo_red_dark), Contextcompat.getcolor (this, Android. R.color.white));
Set the color of a line
Mtabtl.setselectedtabindicatorcolor (Contextcompat.getcolor (this, Android. R.color.holo_green_dark));
Set the spacing between tabs px
Viewcompat.setelevation (MTABTL, 10);
Viewpager corresponding to the correlation
Mtabtl.setupwithviewpager (MCONTENTVP);
}

private void SetContent () {
Tabindicators = new arraylist<> ();
for (int i = 0; i < 3; i++) {
Tabindicators.add ("tab" + i);
}
tabfragments = new arraylist<> ();
for (String s:tabindicators) {
Tabfragments.add (Tabcontentfragment1.newinstance (s));
}
Contentadapter = new Contentpageradapter (Getsupportfragmentmanager ());
Mcontentvp.setadapter (Contentadapter);
}

private void Setview () {
MTABTL = (tablayout) Findviewbyid (R.id.tl_tab);
MCONTENTVP = (Viewpager) Findviewbyid (r.id.vp_content);
}

private void Setlistener () {

}

Class Contentpageradapter extends Fragmentpageradapter {

Public Contentpageradapter (fragmentmanager FM) {
Super (FM);
}

@Override
Public Fragment getItem (int position) {
return Tabfragments.get (position);
}

@Override
public int GetCount () {
return Tabindicators.size ();
}

Set the caption of the label indicator
@Override
Public charsequence getpagetitle (int position) {
return Tabindicators.get (position);
}

}
}


See: http://www.jianshu.com/p/39a66373498c

More detailed http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0731/3247.html including custom encapsulation tablayout

A lot of settings can be made
 <style name= "mycustomtablayout" parent= "Widget.Design.TabLayout"; 
<item name= "Tabmaxwidth" > 80dp</item> tablayout Maximum width
<item name= "tabindicatorcolor" >?attr/colorAccent</item> indicator color
<item name= "Tabindicatorheight" >12dp</item> the height of the indicator
<item name= "Tabpaddingstart" >12dp< /item> tab left margin
<item name= "tabpaddingend" >12dp</item> right margin
<item name= "Tabbackground" >@a ndroid:color/holo_blue_bright</item> tablayout background color
<item name= "tabtextappearance" > @style/ mycustomtabtextappearance</item> Setting the font style
<item name= "Tabselectedtextcolor" >?android: Textcolorprimary</item>
</style>
<style name= "mycustomtabtextappearance" parent= " TextAppearance.Design.Tab "
<item name=" Android:textsize ">14sp</item> set the font size in the indicator
<item n Ame= "Android:textcolor" >?android:textColorSecondary</item> set the color of the font in the indicator

<item name= "Textallcaps" >true</item> whether to force font to uppercase
</style>

Android Indicator Tablatyout

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.