TabLayout设置字体

来源:互联网 发布:2019高考倒计时软件 编辑:程序博客网 时间:2024/06/16 11:55

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" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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

<style name="TabLayoutTextStyle">        <item name="android:textSize">@dimen/text_16_sp</item></style>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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

app:tabIndicatorHeight="0dp"
0 0
原创粉丝点击