Android中修改TabLayout底部导航条Indicator长短的方法
程序员文章站
2023-12-09 20:45:45
前言
对于tablayout相信大家都不陌生,在开发中使用的应该很频繁了,但是底部导航条长短是固定死的,需要自己来改动长短,找了半天没找着方法,看了下官方建议,可以通过映...
前言
对于tablayout相信大家都不陌生,在开发中使用的应该很频繁了,但是底部导航条长短是固定死的,需要自己来改动长短,找了半天没找着方法,看了下官方建议,可以通过映射来修改自己想要的长短,其实也就几行代码的问题。
看代码:
public static void setindicator(context context, tablayout tabs, int leftdip, int rightdip) { 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(); } int left = (int) (getdisplaymetrics(context).density * leftdip); int right = (int) (getdisplaymetrics(context).density * rightdip); for (int i = 0; i < ll_tab.getchildcount(); i++) { view child = ll_tab.getchildat(i); child.setpadding(0, 0, 0, 0); linearlayout.layoutparams params = new linearlayout.layoutparams(0, linearlayout.layoutparams.match_parent, 1); params.leftmargin = left; params.rightmargin = right; child.setlayoutparams(params); child.invalidate(); } }
leftdip和rightdip是左右间距,根据自己的需要来设置,直接调用即可。
很多同学都遇到问题,这里附上源码,大家可以看看:
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 class mainactivity extends appcompatactivity { private static int tab_margin_dip = 11; tablayout tablelayout; viewpager viewpager; rankingpageradapter adapter; string [] strings = {"皇帝","丞相","尚书","太监"}; @override protected void oncreate(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 = new rankingpageradapter(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 class rankingpageradapter extends fragmentstatepageradapter { public rankingpageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return new fuelfragment().newinstance(position, strings[position]); } @override public int getcount() { return strings.length; } @override public charsequence getpagetitle(int position) { return strings[position]; } } public static void setindicator(context context, tablayout tabs, int leftdip, int rightdip) { 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(); } int left = (int) (getdisplaymetrics(context).density * leftdip); int right = (int) (getdisplaymetrics(context).density * rightdip); for (int i = 0; i < ll_tab.getchildcount(); i++) { view child = ll_tab.getchildat(i); child.setpadding(0, 0, 0, 0); linearlayout.layoutparams params = new linearlayout.layoutparams(0, linearlayout.layoutparams.match_parent, 1); params.leftmargin = left; params.rightmargin = right; child.setlayoutparams(params); child.invalidate(); } } public static displaymetrics getdisplaymetrics(context context) { displaymetrics metric = new displaymetrics(); ((activity) context).getwindowmanager().getdefaultdisplay().getmetrics(metric); return metric; } public static float getpxfromdp(float value, context context) { return typedvalue.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 class fuelfragment extends fragment { public static string tab_position; public static string tab_name; private string tabid; private int tabposition; public fuelfragment() { } public static fragment newinstance(int tabposition, string tabname) { fuelfragment fragment = new fuelfragment(); bundle bundle = new bundle(); bundle.putint(tab_position, tabposition); bundle.putstring(tab_name, tabname); fragment.setarguments(bundle); return fragment; } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.layout, container, false); } @override public void onactivitycreated(@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); } }
布局:
<?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>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。