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

原创:转载请标明出处:
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"

最后上一张效果图:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ming2316780/article/details/51763864