Android中修改TabLayout底部导航条Indicator长短的方法

Android中修改TabLayout底部导航条Indicator长短的方法,第1张

概述前言对于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.@R_419_1715@();  }  tabStrip.setAccessible(true);  linearLayout ll_tab = null;  try {   ll_tab = (linearLayout) tabStrip.get(tabs);  } catch (illegalaccessexception e) {   e.@R_419_1715@();  }  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);   linearLayout.LayoutParams params = new linearLayout.LayoutParams(0,linearLayout.LayoutParams.MATCH_PARENT,1);   params.leftmargin = left;   params.rightmargin = right;   child.setLayoutParams(params);   child.invalIDate();  } }@H_502_16@

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,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()); }}@H_502_16@

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); }}@H_502_16@

布局:

<?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>@H_502_16@

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

总结

以上是内存溢出为你收集整理的Android中修改TabLayout底部导航条Indicator长短的方法全部内容,希望文章能够帮你解决Android中修改TabLayout底部导航条Indicator长短的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146096.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存