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

所属分类: 软件编程 / Android 阅读数: 35
收藏 0 赞 0 分享

前言

对于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>

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

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多