当前位置:网站首页>TabLayout 使用详解(修改文字大小、下划线样式等)
TabLayout 使用详解(修改文字大小、下划线样式等)
2022-06-10 12:55:00 【yechaoa】
目录
效果:
官网是这样介绍的:
TabLayout provides a horizontal layout to display tabs. (水平方向的选项卡)
依赖:
implementation 'com.google.android.material:material:1.2.1'代码方式:
TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));XML方式:
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.TabItem
android:text="@string/tab_text"/>
<android.support.design.widget.TabItem
android:icon="@drawable/ic_android"/>
</android.support.design.widget.TabLayout>关联ViewPager:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="@color/red"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/black"
app:tabTextColor="@color/gray"/>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>Java代码:
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mViewPager = (ViewPager) findViewById(R.id.view_pager);//设置adapter
mViewPager.setAdapter(new SimpleFragmentPagerAdapter(getSupportFragmentManager()));
//关联viewpager
mTabLayout.setupWithViewPager(mViewPager);private class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
private String tabTitles[] = new String[]{"tab1", "tab2", "tab3"};
private Fragment[] mFragment = new Fragment[]{new Fragment1(), new Fragment2(), new Fragment3()};
private SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragment[position];
}
@Override
public int getCount() {
return mFragment.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}这样基本能实现大部分需求了
常用属性:
- app:tabIndicatorColor="@color/red" 指示器的颜色
- app:tabIndicatorHeight 指示器的高度,去掉指示器的话直接设置0dp
- app:tabMode="fixed" 显示的模式,fixed表示平分显示,scrollable滑动显示
- app:tabSelectedTextColor="@color/black" 选中文字颜色
- app:tabTextColor="@color/gray" 未选中文字颜色
- app:tabMinWidth="50dp" 最小宽度,可以控制tab的宽度,包括指示器的宽度
- app:tabMaxWidth="100dp" 最大宽度
所有属性:
高级用法:
设置图标
mTabLayout.getTabAt(0).setIcon(R.mipmap.ic_launcher);添加监听
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//选中
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//未选中
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
//再次选中
}
});默认选中或指定选中
mTabLayout.getTabAt(position).select();关联ViewPager的话选中Viewpager也是一样的
mViewPager.setCurrentItem(position);文字大小、样式
app:tabTextAppearance="@style/MyTabLayout" <!--TabLayout字体大小-->
<style name="MyTabLayout">
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
</style>textAllCaps 设置大小写下划线宽度等同文字
app:tabIndicatorFullWidth="false"下划线样式
app:tabIndicator="@drawable/shape_tab_indicator"<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="15dp"
android:height="5dp"
android:gravity="center">
<shape>
<corners android:radius="5dp" />
<!--color无效,源码用tabIndicatorColor-->
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>宽度、高度、圆角等
Github:
边栏推荐
- If I write the for loop again, I will hammer myself
- MySQL master database operation large table DDL, slave database crash and system parameter error setting
- Cvpr2022|aconvnetforthe2020s & how to design neural network Summary
- Site investigation system
- Can qiniu open an account? Is it safe to open an account in qiniu
- 從解讀 BDC 自動生成的代碼談起,講解 SAPGUI 的程序組成部分
- Final exam - Principles of compilation
- Tidb elementary course experience 8 (cluster management and maintenance, adding a tikv node)
- Google proposed the super pre training model coca, and the accuracy of fine-tuning top-1 on Imagenet reached 91%! SOTA on multiple downstream tasks!
- 如何才能把团队给带解散。。。
猜你喜欢

NanoMQ Newsletter 2022-05|v0.8.0 发布,新增 WebHook 拓展接口和连接认证 API

Leetcode 96. Different binary search trees

If I write the for loop again, I will hammer myself

日本版arXiv凉得一批:2个多月了,才收到37篇论文

Which EDA design software should Altium Allegro pads choose

SAP Field Service Management 和微信集成的案例分享和实现介绍

"Reduce the burden" so that the "pig" can fly higher

Comprehensive training of large projects

The Japanese version of arXiv is a cool batch: only 37 papers have been received after more than 2 months

汇编语言入门-总结
随机推荐
[mobile robot] principle of wheel odometer
Let resources flow freely in the cloud and locally
【无标题】音频蓝牙语音芯片,WT2605C-32N实时录音上传技术方案介绍
Development trend of Web Development
JS global timer case
施一公等团队登Science封面:AI与冷冻电镜揭示「原子级」NPC结构,生命科学突破
20-year technical veteran gives up his CTO title! Why did the startup attract him?
mTabLayout. setOnTabSelectedListener is deprecated
In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
Introduction to assembly language - Summary
Google proposed the super pre training model coca, and the accuracy of fine-tuning top-1 on Imagenet reached 91%! SOTA on multiple downstream tasks!
深度神经网络每秒分类近20亿张图像,新型类脑光学分类器芯片登上Nature
十款好用跨浏览器测试工具分享,好物值得收藏
CL210OpenStack操作的故障排除--常见核心问题的故障排除
WT2003H4-16S 语音芯片按键录音及播放应用解析
Neuron Newsletter 2022-05|新增 2 個南向驅動和 1 個北向應用、Modbus TCP 實現定制擴展
Qt: 访问其他窗体中的控件
【深度学习】基于深度学习Autoencoder的信用卡欺诈异常检测,效果非常牛逼
百度程序员删库被判9个月,手机号一键解绑功能发布,推特再向马斯克妥协,今日更多大新闻在此...
[golang] when creating a structure with configuration parameters, how should the optional parameters be transferred?