android tablayout 自定义,TabLayout用法详解及自定义样式

本文详细解析了TabLayout的默认样式、基本用法,包括静态和动态添加Tab,以及与ViewPager的联动。同时介绍了如何自定义TabLayout的样式,如修改Indicator长度,并展示了如何创建自定义TabItem并监听其点击事件。
摘要由CSDN通过智能技术生成

TabLayout的默认样式:

app:theme="@style/Widget.Design.TabLayout"

从系统定义的该样式继续深入:

fill

fixed

264dp

?attr/colorAccent

2dp

12dp

12dp

?attr/selectableItemBackground

@style/TextAppearance.Design.Tab

?android:textColorPrimary

接着,看看系统定义Tab文本的样式(注意textAllcaps这个属性):

14dp

?android:textColorSecondary

true

从系统定义TabLayout的默认样式可以看出,我们可以改变TabLayout对应的系统样式的属性值来适配我们自己的需求.

TabLayout的基本用法

TabLayout独立使用使用时,可以xml布局中静态添加tab个数及其样式,也可以动态添加Tab的个数及其样式,如:

android:id="@+id/tablayout"

android:background="@color/colorPrimary"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Android"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:icon="@mipmap/ic_launcher"/>

bc9a6927ab2177bd645c85135d6f3438.png

或者:

android:id="@+id/tablayout"

android:background="@color/colorPrimary"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

private int[] images = new int[]{

R.drawable.ic_account_balance_wallet_black,

R.drawable.ic_android_black,

R.drawable.ic_account_box_black};

private String[] tabs = new String[]{"小说", "电影", "相声"};

TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);

tabLayout.addTab(tabLayout.newTab().setIcon(images[0]).setText(tabs[0]),true);

tabLayout.addTab(tabLayout.newTab

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值