Android Tablayout Combat

Source: Internet
Author: User

1.

2. Principle: Tablayout provides a horizontal layout to show the tabs

3. (Explore one: Some posts say to add dependency: Compile ' com.android.support:design:25.3.1 '

But I did not add this dependency, can still run without error)

Layout file:

<android.support.design.widget.tablayout

Android:id= "@+id/tablayout"

Android:layout_width= "Match_parent"

android:layout_height= "30DP"

app:tabindicatorheight= "2DP"//Set note underline height (thickness) is not set, the default size can be

App:tabindicatorcolor= "#007aff"//sets the color of the label underline

App:tabselectedtextcolor= "#007aff"//Set the color of the selected label's font

App:tabtextcolor= "@android: Color/darker_gray"/>//sets the color of the font for unchecked labels

<android.support.v4.view.viewpager

Android:id= "@+id/vpager"

Android:layout_width= "Match_parent"

android:layout_height= "200DP"/>

Controller Part code:

A

class Tabadapter extends Fragmentpageradapter {

Public Tabadapter (fragmentmanager FM) {
super (FM);
}

@Override
Public Fragment GetItem (int position) {
return Fragments.get (position);
}

@Override
public int GetCount () {
return Fragments.size ();
}
//Display the text on the label
@Override
Public charsequence getpagetitle (int position) {
return Tabs.get (position);
}
}

Two

Public Static Class Tabframent extends Fragment {
private context context;
private String content;

Public tabframent () {
}

@SuppressLint ("Validfragment")
Public tabframent (Context contexts, String content) {
this.context = contexts;
this.content = content;
}

@Nullable
@Override
Public View Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedinstanc EState) {
TextView TextView = new TextView (context); Here, for example, you can lay out any layout, customize the layout, and use a TextViewView view = Inflater.inflate ()Method
             Textview.settext (content);
Textview.settextsize (30);
Textview.setgravity (Gravity.center);
return textView;

}
}

Three

  private void InitData () { 

for (map& Lt String,string> map:list) {//data obtained from the server has been processed into list
Tabs.add (map.get       ("tab")); tab label
Fragments.add (new tabframent ("content")); This,map.get GE content
}
Tablayout = (Tablayout) fi Ndviewbyid (r.id.tablayout);

//set Tablayout mode
Tablayout.settabgravity (Tablayout.gravity_center); //Note: This property must be used with mode_fixed, otherwise it will not work
// Gravity_fill   Let each label divide tablayout all width

//gravity_center  let each label display its own width, then all labels are centered on the


Tablayout.settabmode (tablayout.mode_scrollable);

Mode_scrollable means that each label maintains its own width, and once the label is too large, it gives the title bar the ability to support horizontal sliding.


Tabadapter tabadapter=new Tabadapter (Getsupportfragmentmanager ());
Vpager.setadapter (Tabadapter);

Associated Viewpager and Tablayout
Tablayout.setupwithviewpager (Vpager);


        Sets     the vertical split line between the split line labels        linearlayout linear = (linearlayout) tablayout.getchildat (0);        Linear.setshowdividers (linearlayout.show_divider_middle);        Linear.setdividerdrawable (Contextcompat.getdrawable (This,r.drawable.divider));        Set the split line interval        linear.setdividerpadding (dip2px (15));
}
  
Four
public int dip2px (int dip) { float density = getresources (). Getdisplaymetrics (). density; return (int) (DIP * density + 0.5); }

Android Tablayout Combat

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.