当前位置:网站首页>实现 TabLayout 下标与文字等长,选中字体大小改变
实现 TabLayout 下标与文字等长,选中字体大小改变
2022-06-11 21:17:00 【我的安卓之路】
android 使用系统TabLayout 实现 下标与文字等长效果
android 使用系统TabLayout 实现改变Tab选中状态样式
void setTabLyout(){
setTabPadding(tablayout,resetMeasureW(7));
tablayout.addOnTabSelectedListener(onTabSelectedListener);
}
TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
setTabSelect(tab);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.setCustomView(null);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
void setTabSelect(TabLayout.Tab tab) { //设置选中后显示的布局
TextView textView = new TextView(getActivity());
textView.setTextSize(18);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.bgf_textred));
textView.setText(tab.getText());
TextPaint tp = textView.getPaint();
tp.setFakeBoldText(true);
tab.setCustomView(textView);
}
/**
* 动态计算文字宽度设置指示器宽度
* @param tabLayout
* @param padding 相邻tab的间距
*/
public static void setTabPadding(final TabLayout tabLayout, final int padding) {
tabLayout.post(new Runnable() {
@Override
public void run() {
//拿到tabLayout的mTabStrip属性
LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
View tabView = mTabStrip.getChildAt(i);
//拿到tabView的mTextView属性 tab的字数不固定一定用反射取mTextView
tabView.setPadding(0, 0, 0, 0);
//设置tab左右间距 注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
params.leftMargin = padding;
params.rightMargin = padding;
tabView.setLayoutParams(params);
tabView.invalidate();
}
}
});
}
/**
* 按750设计图 比例 获取实际宽度
*/
public static int resetMeasureW(int w) {
int screenWidth = getScreenWidth(getApplication());
double mW = w * screenWidth / 375.0;
return (int) mW;
}
//屏幕分辨率
public static DisplayMetrics getDisplayMetrics(Context context) {
return context.getResources().getDisplayMetrics();
}
//屏幕宽度
public static int getScreenWidth(Context context) {
return getDisplayMetrics(context).widthPixels;
}边栏推荐
- Ubantu1804 two opencv versions coexist
- 周刊02|不瞒你说,我其实是MIT的学生
- JVM之堆区
- RANSAC extract cylinder (matlab built-in function)
- Solution to the problem of PHP strtotime obtaining natural monthly error
- In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
- Modify appid of local wechat applet
- Compilation process of program
- [data visualization] Apache superset 1.2.0 tutorial (II) - Quick Start (visualizing King hero data)
- 电竞网咖用2.5G网卡,体验飞一般的感觉!
猜你喜欢

JVM object allocation policy TLAB

从概率论基础出发推导卡尔曼滤波

Role of RESNET residual block

Tensorflow 2. X Getting Started tutorial

【数据可视化】Apache Superset 1.2.0教程 (二)——快速入门(可视化王者英雄数据)

重投农业,加码技术服务,拼多多底盘进一步夯实

Explanation of each column output by explain statement

The secret of derating - don't challenge Datasheet

JVM method area

What are striplines and microstrip lines? Reference planes and transmission lines
随机推荐
Object creation process of JVM
How to manually drag nodes in the Obsidian relationship graph
新品发布:国产单电口千兆网卡正式量产!
【博弈论-绪论】
Deploy website traffic statistics background based on Tencent cloud lightweight application server and umami
新品发布:LR-LINK联瑞推出首款25G OCP 3.0 网卡
One article to show you how to understand the harmonyos application on the shelves
In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
Goto statement of go language
Weekly 02 | pour être honnête, je suis un étudiant du MIT
Application business layer modification
Why is your LDO output unstable?
JVM method area
Add personal statement for go file in file template in Golan
Online excel file parsing and conversion to JSON format
Frequency domain filter
Application analysis of Poe image acquisition card in machine vision industrial computer
Pyqt5 technical part - set the default value of qcombobox drop-down box and get the current selection of the drop-down box
关于gorm的preload方法笔记说明
应用业务层修改