|
| 1 | +# XTabLayout——可修改选中项字体大小和指示器长度的TabLayout |
| 2 | +## XTabLayout是基于design包中的TabLayout进行了功能的扩展,在保留原有功能的基础上,增加了修改选中项字体大小、修改指示器长度以及限制屏幕显示范围内显示的Tab个数。 |
| 3 | + |
| 4 | +## 集成步骤: |
| 5 | +### 1.添加XTabLayout依赖库 |
| 6 | +#### 在app目录下的build.gradle的dependencies中添加如下引用: |
| 7 | + |
| 8 | + compile 'com.androidkun:XTabLayout:1.0.1' |
| 9 | + |
| 10 | +### 2.在布局文件中设置XTabLayout属性 |
| 11 | + |
| 12 | + <com.androidkun.xtablayout.XTabLayout |
| 13 | + android:id="@+id/xTablayout" |
| 14 | + android:layout_width="match_parent" |
| 15 | + android:layout_height="50dp" |
| 16 | + app:xTabMode="scrollable" |
| 17 | + app:xTabIndicatorColor="#0f0" |
| 18 | + app:xTabIndicatorHeight="4dp" |
| 19 | + app:xTabIndicatorWidth="15dp" |
| 20 | + app:xTabSelectedTextColor="#f00" |
| 21 | + app:xTabTextColor="#000" |
| 22 | + app:xTabTextSize="15sp" |
| 23 | + app:xTabSelectedTextSize="20sp"/> |
| 24 | + |
| 25 | +#### TabLayout有的属性,在XTabLayout中都会有,属性名称都是前面加个x,后面的t变成大写。 |
| 26 | +#### 其中增加了xTabIndicatorWidth用于设置指示器长度,xTabTextSize用于设置未选中项的字体大小,xTabSelectedTextSize用于设置选中项的字体大小。 |
| 27 | +#### 此外1.0.1版本中添加如下属性可以设置屏幕范围内显示的Tab个数 |
| 28 | + |
| 29 | + app:xTabDisplayNum="3" |
| 30 | + |
| 31 | +#### 或者在代码中添加 |
| 32 | + |
| 33 | + tabLayout.setxTabDisplayNum(3);//需要写在setupWithViewPager前 |
| 34 | + tabLayout.setupWithViewPager(viewPager); |
| 35 | + |
| 36 | +#### 这里我们限制为3个,则每个tab的宽度为屏幕的1/3,显示效果如下: |
| 37 | +  |
| 38 | + |
| 39 | +#### 需要注意显示的个数会受Adapter的ItemCount影响,例如ItemCount为3,但是我们设置app:xTabDisplayNum=“4”,那么显示出来的Tab的宽度其实是屏幕的1/3,并非1/4。 |
| 40 | + |
| 41 | +### 3.初始化 |
| 42 | +#### XTabLayout的使用方式和TabLayout是一样的,代码如下: |
| 43 | + |
| 44 | + //将TabLayout和ViewPager关联起来。 |
| 45 | + XTabLayout tabLayout = (XTabLayout) findViewById(R.id.xTablayout); |
| 46 | + tabLayout.setupWithViewPager(viewPager); |
| 47 | + |
| 48 | +#[CSDN地址](http://blog.csdn.net/a1533588867/article/details/53810409) |
| 49 | + |
0 commit comments