Android中TabLayout切换选项背景和修改字体大小

本文介绍了如何在Android中自定义TabLayout的选项背景和字体大小。通过设置`app:tabBackground`和`app:tabTextAppearance`属性,可以实现切换背景的改变和文字大小的调整。同时,文章提到了为了实现块状切换效果,可以将`app:tabIndicatorHeight`设为0dp。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创:转载请标明出处:
http://blog.csdn.net/ming2316780/article/details/51763864
本文出自:【Android_Jerry的博客】

TabLayout切换选项背景和修改字体大小

首先呢,TabLayout是Android5.0后出的新控件,使用需要导入design包,一般情况下,TabLayout需要和ViewPager搭配使用。

在TabLayout标签属性中,只有改变切换文字颜色属性的,就连文字大小的设置也没有直接属性可设置。

其实设置很简单,只需要设置 app:tabBackground 和 app:tabTextAppearance 的属性,即可实现。

TabLayout xml设置代码如下:

<android.support.design.widget.TabLayout
            android:id="@+id/tablayout_video"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tablayout_height"
            app:tabBackground="@drawable/tablayout_background"
            app:tabIndicatorHeight="0dp"
            app:tabSelectedTextColor="@color/white"
            app:tabTextAppearance="@style/TabLayoutTextStyle"
            app:tabTextColor="@color/white" />

app:tabBackground 属性:此处要写一个selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_horizontal_item_p" android:state_selected="true" />
    <item android:drawable="@drawable/tab_horizontal_item_d" />
</selector>

tab_horizontal_item_d (tab_horizontal_item_d) tab_horizontal_item_p (tab_horizontal_item_p)

app:tabTextAppearance 属性:此处要写一个style

<style name="TabLayoutTextStyle">
        <item name="android:textSize">@dimen/text_16_sp</item>
</style>

最后说一下,因为TabLayout在布局底部是自带一条滚动条的,为了实现块状切换的效果,可以把 app:tabIndicatorHeight 属性设置为0dp,去掉滚动条

app:tabIndicatorHeight="0dp"

最后上一张效果图:

这里写图片描述

评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值