Google in the 2015 IO Conference, brought us more detailed material design specifications, but also brought us a new Android design support Library, in this support repository, Google provides us with a more standard MD design-style control. Most importantly, the Android Design Support Library is more compatible and can be directly backwards compatible to Android 2.2. It has to be said to be a conscience.
The way to use it is simple, just add a dependency
Compile ' com.android.support:design:24.0.0 '
In the next few days we will learn a couple of new controls together,
The first thing to bring is tablayout.
tab swipe to switch view is not a new concept, but Google is the first time in the support library to provide complete supporting,
Furthermore, the tablayout of the Design library implements a fixed tab-the average width distribution of the view,
A scrollable tab is also implemented-the view width is not fixed and can be scrolled horizontally. tab can be added dynamically in the program,
But most of the time we will not use this, usually the sliding layout will be used in conjunction with Viewpager, so we need Viewpager to help:
By a word setupwithviewpager, we put Viewpager and tablayout together.
Last Run Diagram:
Look at the code
First, the XML file on the main page
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "xmlns:app="/HTTP/ Schemas.android.com/apk/res-auto "android:layout_width=" match_parent "android:layout_height=" Match_parent "Androi d:orientation= "vertical" tools:context= "com.example.nanchen.designtablayoutdemo.MainActivity" > < Android.support.design.widget.TabLayout android:layout_width= "match_parent" app:tabbackground= "@color/colorp Rimarydark "app:tabtextcolor=" @android: Color/white "app:tabselectedtextcolor=" #04b4ae "android:layout _height= "Wrap_content" android:id= "@+id/main_tab_layout"/> <android.support.v4.view.viewpager Andro Id:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" android:id= "@+id /MAIN_VP "/></LINEARLAYOUT>
Where the Tablayout control is under the design package, you must add
xmlns:app= "Http://schemas.android.com/apk/res-auto"
This allows you to use the custom properties of it. It has a lot of properties, so simple to use a few, more people can go to try.
Here, I only scroll three, if your app tab tag more,
App:tabmode= "Scrollable"
This attribute must be specified, otherwise you will find that it is not visible, if the number of tags is small, it is recommended to set to fixed, that will be equal.
Next is the fragment XML file
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= "Com.example.nanchen.designtablayoutdemo.BlankFragment" > <!--todo:update Blank Fragment Layout-- <textview android:layout_width= "match_parent" android:layout_height= "match _parent " android:textsize=" 30sp " android:gravity=" center " android:id=" @+id/fg_text " android: text= "@string/hello_blank_fragment"/></framelayout>
Mainactivity.java
package Com.example.nanchen.designtablayoutdemo;import Android.os.Bundle; Import Android.support.design.widget.tablayout;import Android.support.v4.view.viewpager;import Android.support.v7.app.appcompatactivity;import Java.util.arraylist;import Java.util.list;public Class Mainactivity extends Appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate) {Super . OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Viewpager VP = (viewpager) Findviewbyid (R.ID.MAIN_VP); Tablayout tablayout = (tablayout) Findviewbyid (r.id.main_tab_layout); list<string> list = new arraylist<> (); for (int i = 1; i < 4; i++) {//List.add (String.Format (Locale.china, "02d% page", i)); List.add ("+i+" page "); } vp.setadapter (New Myadapter (Getsupportfragmentmanager (), list)); Tablayout.setupwithviewpager (VP); }}
Fragment is used to hold the following data
Package Com.example.nanchen.designtablayoutdemo;import Android.os.bundle;import Android.support.annotation.nullable;import Android.support.v4.app.fragment;import Android.view.LayoutInflater; Import Android.view.view;import android.view.viewgroup;import android.widget.textview;/** * A simple {@link Fragment} Subclass. */public class Blankfragment extends Fragment {public blankfragment () {//Required empty public constructor public static blankfragment newinstance (String text) {Bundle args = new Bundle (); Args.putstring ("text", text); Blankfragment fragment = new Blankfragment (); Fragment.setarguments (args); return fragment; } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundl E savedinstancestate) {//Inflate the layout for this fragment return Inflater.inflate (R.LAYOUT.FRAGMENT_BL Ank, container, false); } @Override public void OnviewcreatEd (view view, @Nullable Bundle savedinstancestate) {super.onviewcreated (view, savedinstancestate); TextView Text = (TextView) View.findviewbyid (R.id.fg_text); String str = getarguments (). getString ("text"); Text.settext (str); }}
Need a Pageradapter
Customize one.
Package Com.example.nanchen.designtablayoutdemo;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;import Java.util.List ;/** * Created by the southern dust on 2016/7/12. */public class Myadapter extends Fragmentpageradapter { private list<string> List; Public Myadapter (Fragmentmanager FM, list<string> List) { super (FM); this.list = list; } @Override public Fragment getItem (int position) { return blankfragment.newinstance (list.get (position)); } @Override public int GetCount () { return list.size (); } @Override public charsequence getpagetitle (int position) { return list.get (position);} }
Simple use of the Tablayout control of the Android design package