About Tablayout, the use should be very frequent, but the bottom of the navigation bar length is fixed dead, need to change the length of their own, looking for a half-day did not find a way to see the official recommendations, you can change the length of the map to modify their own, in fact, a few lines of code problems, see the code:
Public Static voidSetindicator (Context context, tablayout tabs,intLeftdip,intRightdip) {Class<?> tablayout =Tabs.getclass (); Field TabStrip=NULL; Try{TabStrip= Tablayout.getdeclaredfield ("Mtabstrip"); } Catch(nosuchfieldexception e) {e.printstacktrace (); } tabstrip.setaccessible (true); LinearLayout Ll_tab=NULL; Try{Ll_tab= (linearlayout) TabStrip.Get(tabs); } Catch(illegalaccessexception e) {e.printstacktrace (); } intleft = (int) (Getdisplaymetrics (context). Density *Leftdip); intright = (int) (Getdisplaymetrics (context). Density *Rightdip); for(inti =0; I < Ll_tab.getchildcount (); i++) {View child=Ll_tab.getchildat (i); Child.setpadding (0,0,0,0); Linearlayout.layoutparamsparams=NewLinearlayout.layoutparams (0, LinearLayout.LayoutParams.MATCH_PARENT,1); params. LeftMargin =Left ; params. RightMargin =Right ; Child.setlayoutparams (params); Child.invalidate (); } }
Leftdip and Rightdip is the left and right spacing, according to their own needs to set, direct call can be.
Many students have encountered problems, here is attached source code, we can see: Package Com.example.donghe.myrefreshview;import Android.app.activity;import Android.content.context;import Android.support.design.widget.tablayout;import android.support.v4.app.Fragment; Import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentstatepageradapter;import Android.support.v4.view.viewpager;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle; Import Android.util.displaymetrics;import Android.util.typedvalue;import Android.view.view;import Android.widget.linearlayout;import Android.widget.tablelayout;import Java.lang.reflect.Field; Public classMainactivity extends Appcompatactivity {Private Static intTab_margin_dip = One; Tablayout tablelayout; Viewpager Viewpager; Rankingpageradapter adapter; String [] Strings= {"Emperor","Prime Minister","Shang","Eunuch"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tablelayout=(tablayout) Findviewbyid (r.id.tabs_ranking); Viewpager=(Viewpager) Findviewbyid (r.id.container_ranking); Adapter=NewRankingpageradapter (Getsupportfragmentmanager ()); Viewpager.setadapter (adapter); Tablelayout.setupwithviewpager (Viewpager); Tablelayout.settabtextcolors (Getresources (). GetColor (R.color.black), Getresources (). GetColor (R.color.red_pre)); Tablelayout.settabsfrompageradapter (adapter); Setindicator ( This, Tablelayout, Tab_margin_dip, Tab_margin_dip); } Private classRankingpageradapter extends Fragmentstatepageradapter { PublicRankingpageradapter (fragmentmanager FM) {super (FM); } @Override PublicFragment GetItem (intposition) { return Newfuelfragment (). newinstance (position, strings[position]); } @Override Public intGetCount () {returnstrings.length; } @Override PublicCharsequence Getpagetitle (intposition) { returnStrings[position]; } } Public Static voidSetindicator (Context context, tablayout tabs,intLeftdip,intRightdip) {Class<?> tablayout =Tabs.getclass (); Field TabStrip=NULL; Try{TabStrip= Tablayout.getdeclaredfield ("Mtabstrip"); } Catch(nosuchfieldexception e) {e.printstacktrace (); } tabstrip.setaccessible (true); LinearLayout Ll_tab=NULL; Try{Ll_tab= (linearlayout) TabStrip.Get(tabs); } Catch(illegalaccessexception e) {e.printstacktrace (); } intleft = (int) (Getdisplaymetrics (context). Density *Leftdip); intright = (int) (Getdisplaymetrics (context). Density *Rightdip); for(inti =0; I < Ll_tab.getchildcount (); i++) {View child=Ll_tab.getchildat (i); Child.setpadding (0,0,0,0); Linearlayout.layoutparamsparams=NewLinearlayout.layoutparams (0, LinearLayout.LayoutParams.MATCH_PARENT,1); params. LeftMargin =Left ; params. RightMargin =Right ; Child.setlayoutparams (params); Child.invalidate (); } } Public StaticDisplaymetrics Getdisplaymetrics (context context) {Displaymetrics metric=NewDisplaymetrics (); (Activity) context). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric); returnmetric; } Public Static floatGETPXFROMDP (floatvalue, Context context) { returntypedvalue.applydimension (Typedvalue.complex_unit_dip, Value, Context.getresources (). getdisplaymetrics ()); }}
Fragment:
Package Com.example.donghe.myrefreshview;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;/** * Created by dong.he on 2016/12/9.*/ Public classFuelfragment extends Fragment { Public StaticString tab_position; Public StaticString Tab_name; PrivateString tabId; Private inttabposition; Publicfuelfragment () {} Public StaticFragment newinstance (inttabposition, String tabname) {Fuelfragment Fragment=Newfuelfragment (); Bundle Bundle=NewBundle (); Bundle.putint (Tab_position, tabposition); Bundle.putstring (Tab_name, tabname); Fragment.setarguments (bundle); returnfragment; } @Nullable @Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { returnInflater.inflate (R.layout.layout, container,false); } @Override Public voidonactivitycreated (@Nullable Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); TabPosition= Getarguments (). GetInt (Tab_position,0); TabId=getarguments (). getString (Tab_name); ((TextView) GetView (). Findviewbyid (R.id.text)). SetText (TabId); }}
Layout:
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <android.support.design.widget.TabLayout Android:id="@+id/tabs_ranking"Android:layout_width="match_parent"Android:layout_height="wrap_content"app:tabgravity="Fill"App:tabindicatorcolor="@color/red_pre"App:tabmode="scrollable"/> <Android.support.v4.view.ViewPager Android:id="@+id/container_ranking"Android:layout_width="match_parent"Android:layout_height="match_parent"/></linearlayout>
Please contact us with questions.
Android modifies tablayout bottom navigation bar indicator length