前面呢,有写过TabLayout的博客,最近开发用到了高本版遇到一些问题,来总结一下

Android--------TabLayout实现新闻客户端顶部导航栏

Android中Tablayout设置下划线宽度 和 dp和px之间进行相互转换

上面是Api28版本之前是没问题的 api28之后呢,有些地方就有所改变了

public static void reflex(final TabLayout tabLayout){
tabLayout.post(() -> {
try {
//拿到tabLayout的slidingTabIndicator属性
Field tabIndicator = tabLayout.getClass().getDeclaredField("slidingTabIndicator");
//API28以下为mTabStrip
// Field tabIndicator = tabLayout.getClass().getDeclaredField("mTabStrip");
tabIndicator.setAccessible(true);
LinearLayout mTabStrip = (LinearLayout) tabIndicator.get(tabLayout);
int dp10 = dip2px(tabLayout.getContext(), 10); for (int i = 0; i < mTabStrip.getChildCount(); i++) {
View tabView = mTabStrip.getChildAt(i); //拿到tabView的mTextView属性 tab的字数不固定一定用反射取mTextView
Field mTextViewField = tabView.getClass().getDeclaredField("textView");
//API28以下为mTextView
// Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
mTextViewField.setAccessible(true);
TextView mTextView = (TextView) mTextViewField.get(tabView);
tabView.setPadding(0, 0, 0, 0); //字多宽线就多宽,需要测量mTextView的宽度
int width = 0;
width = mTextView.getWidth();
if (width == 0) {
mTextView.measure(0, 0);
width = mTextView.getMeasuredWidth();
} //设置tab左右间距为10dp 这个间距可根据自己需求更改
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
params.width = width ;
params.leftMargin = dp10;
params.rightMargin = dp10;
tabView.setLayoutParams(params);
tabView.invalidate();
} } catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}); }

当然这种方式啊我其实不是很推荐,我在网上也看到一些网友说设置了没效果

所以我用了AndroidX 之后发现了他里面的Tablayout 和之前有点的不太一样了,而且也能实现了下划线问题

请看博客

android ------ 高版本的 Tablayout 下划线宽度的更多相关文章

  1. 隐藏android中EditText中的下划线以及修改光标颜色

    在android开发中 EditTextText是我们经常用到的,我们使用时会有一些小问题,当我们点击输入文字时,EditText中的光标和下划线会变成粉红色. 解决方法很简单,我们只要在EditTe ...

  2. Android-TabLayout设置内容宽度以及下划线宽度

    默认图: 效果图: 项目中使用到需要像今日头条那种实现顶部横向滑动标题功能,本人项目中使用TabLayout+ViewPager实现,但是,实现后默认的TabLayout间距特别大,并且下划线,文字大 ...

  3. Android开发之TextView的下划线添加

    如何给TextView添加下划线呢,最近项目中需要这个,于是就用代码添加了下划线功能.主要就是用Paint的setFlags方法来实现,具体如下: ((TextView)mScrollView.fin ...

  4. android:为TextView加入样式——下划线,颜色,设置链接样式及前背景色

    实现下划线及颜色设置: public class AtActivity extends Activity { LinearLayout ll;     /** Called when the acti ...

  5. Android中Tablayout设置下划线宽度 和 dp和px之间进行相互转换

    开发中遇到了一个问题,Tablayout设置下换线长度,看了点资料,分享给大家. 效果图:               直接贴代码(要在tabLayout添加完所有的tab后调用) public vo ...

  6. TabLayout下划线指示器自适应文字宽度

    解决方案1: 更新design库到28.0.0-rc01 implementation 'com.android.support:design:28.0.0-rc01' 然后在TabLayout里设置 ...

  7. Android 高版本API方法在低版本系统上的兼容性处理

    Android 版本更替,新的版本带来新的特性,新的方法. 新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版本系统上,运行时将会crash. 本文以一个具体的例子说明 ...

  8. Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法

    前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的 ...

  9. android edittext 去边框 去下划线

    EditText的background属性设置为@null就搞定了:android:background="@null"style属性倒是可加可不加 附原文:@SlumberMac ...

随机推荐

  1. [ ERROR ] Error in test library 'pymysql': Creating keyword 'Connect' failed: Keyword with same name defined multiple times.

    [ ERROR ] Error in test library 'pymysql': Creating keyword 'Connect' failed: Keyword with same name ...

  2. 简述mysql问题处理

    最近,有一位同事,咨询我mysql的一点问题, 具体来说, 是如何很快的将一个mysql导出的文件快速的导入到另外一个mysql数据库.我学习了很多mysql的知识, 使用的时间却并不是很多, 对于m ...

  3. Linux chage命令详解

    原文 chage命令用于密码实效管理,该是用来修改帐号和密码的有效期限,接下来通过本文给大家介绍Linux chage命令相关知识,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起学习吧 lin ...

  4. Linux centos关机和重启命令

    Linux centos重启命令: 1.reboot   普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户 ...

  5. 【转】大众点评CAT开源监控系统剖析

    https://www.cnblogs.com/yeahwell/p/cat.html 参考文档: 大众点评的实时监控系统分析(一) CAT_source_analyze 透过CAT,来看分布式实时监 ...

  6. jmeter 测试带token的请求

    https://blog.csdn.net/wd1282988143/article/details/88048114 如果有一个登录接口需要服务端返回参数,再带着这个参数去请求才能完成登录,请问jm ...

  7. DT系统开发之-在线询盘信息的调取实现

    调用标签 <!--{tag("table=message&condition=status=3 and typeid=1&areaid=$cityid&cati ...

  8. 【Spark】ScalaIDE运行spark,A master URL must be set in your configuration

    or SparkSession.master("local")

  9. async await 总结

    1.async await成对出现,await再async定义的函数内 2.async定义的是函数 3.async 返回一个Promise 4.async 函数中 return 的结果将作为回调的参数 ...

  10. 题解 LA2911

    题目大意 多组数据,每组数据给定整数 \(m,p,a,b\),满足 \(a>0\),\(2\leq p\leq12\) 且 \(p\) 为偶数.要求求出一列数 \(x_1,x_2,\cdots, ...