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