android ------ 高版本的 Tablayout 下划线宽度
前面呢,有写过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 下划线宽度的更多相关文章
- 隐藏android中EditText中的下划线以及修改光标颜色
在android开发中 EditTextText是我们经常用到的,我们使用时会有一些小问题,当我们点击输入文字时,EditText中的光标和下划线会变成粉红色. 解决方法很简单,我们只要在EditTe ...
- Android-TabLayout设置内容宽度以及下划线宽度
默认图: 效果图: 项目中使用到需要像今日头条那种实现顶部横向滑动标题功能,本人项目中使用TabLayout+ViewPager实现,但是,实现后默认的TabLayout间距特别大,并且下划线,文字大 ...
- Android开发之TextView的下划线添加
如何给TextView添加下划线呢,最近项目中需要这个,于是就用代码添加了下划线功能.主要就是用Paint的setFlags方法来实现,具体如下: ((TextView)mScrollView.fin ...
- android:为TextView加入样式——下划线,颜色,设置链接样式及前背景色
实现下划线及颜色设置: public class AtActivity extends Activity { LinearLayout ll; /** Called when the acti ...
- Android中Tablayout设置下划线宽度 和 dp和px之间进行相互转换
开发中遇到了一个问题,Tablayout设置下换线长度,看了点资料,分享给大家. 效果图: 直接贴代码(要在tabLayout添加完所有的tab后调用) public vo ...
- TabLayout下划线指示器自适应文字宽度
解决方案1: 更新design库到28.0.0-rc01 implementation 'com.android.support:design:28.0.0-rc01' 然后在TabLayout里设置 ...
- Android 高版本API方法在低版本系统上的兼容性处理
Android 版本更替,新的版本带来新的特性,新的方法. 新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版本系统上,运行时将会crash. 本文以一个具体的例子说明 ...
- Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法
前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的 ...
- android edittext 去边框 去下划线
EditText的background属性设置为@null就搞定了:android:background="@null"style属性倒是可加可不加 附原文:@SlumberMac ...
随机推荐
- 解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in P
转载 解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be i ...
- 浅谈Python设计模式 - 建造者模式
声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 建造者模式 当我们想要创建一个由多个部分构成的对象,而且他们的构建需要一步接一步 ...
- eclipse 小提示
1.模糊提示插件 Code Recommenders alt+/ 模糊提示插件 Code Recommenders
- windows下,tomcat设置为服务启动
一.配置号tomcat 二.cmd命令符下进入tomcat/bin目录,输入:service.bat install 三.运行中输入services.msc 打开服务管理控制台,可以找到 Apache ...
- Nginx 配置及参数详解
Nginx 配置及参数详解 Nginx Location 指令语法 如下就是常用的 location 配置的语法格式,其中modifier是可选的,location_match就是制定 URI 应该去 ...
- CentOS6.7安装部署Nginx(详解主配置文件)
keepalived + nginx :实现高可用 nginx: web服务器 反向代理,也支持缓存(缓存在磁盘上) 支持FastCGI tengine:淘宝官方在nginx原有的代码的基础上对n ...
- NumPy简单入门教程
# NumPy简单入门教程 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体 ...
- php享元模式(flyweight pattern)
周日一大早,今天要送老婆孩子去火车站, 所以练代码要早点哈. <?php /* The flyweight pattern is about performance and resource r ...
- Connected Component in Undirected Graph
Description Find connected component in undirected graph. Each node in the graph contains a label an ...
- LeetCode 865. Smallest Subtree with all the Deepest Nodes
原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...