修改TabLayout下划线的宽度和文字和线的距离

1、写个工具类 封装:

import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import java.lang.reflect.Field;

/**
 * Copyright © 2016AXD. All rights reserved.
 */

public class TabUtils {
    /**
    *  @param tabs TabLayout
     * @param leftDip marginLeft
     * @param rightDip marginRight
    */
   
public static void setIndicator(TabLayout tabs, int leftDip, int rightDip, int bottomDip) {
Class<?> tabLayout = tabs.getClass();
 
Field tabStrip = null;
try
{
tabStrip = tabLayout.getDeclaredField("mTabStrip");
 
} catch (NoSuchFieldException e) {
 e.printStackTrace();
 return;
 }
tabStrip.setAccessible(true);
LinearLayout llTab = null;
 try {
llTab = (LinearLayout) tabStrip.get(tabs);
 
} catch (IllegalAccessException e) {
 e.printStackTrace();
                  return;
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
       int bottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, bottomDip, Resources.getSystem().getDisplayMetrics());

for
(int i = 0; i < llTab.getChildCount(); i++) {

 View child = llTab.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;
           params.bottomMargin = bottom;
  child.setLayoutParams(params);
 child.invalidate();
 }
 }
}

2、在Activity调用

tab_view.post(new Runnable() {
    @Override
    public void run() {
        TabUtils.setIndicator(tab_view,10,10);
    }
});

3、此方法是通过反射获取tablayout私有属性进修改属性值,如果app启动混淆 会报NoSuchFieldException;

解决方案:在proguard-rules.pro混淆文件中添加-keep class Android.support.design.widget.TabLayout{*;}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值