前言:
应用viewpager的时候,一般会使用TabPageIndicator来与其配合,达到很漂亮的效果。但是TabPageIndicator是第三方的,而且比较老了,当然了现在很多大神都已经开始自己写TabPageIndicator来满足自己的需求,在2015年的google大会上,google发布了新的Android Support Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果,而且还是官方的,最好的是它可以兼容到2.2以上版本,包括2.2。
现在一般都是 android studio进行开发的,所以引用TabLayout很简单,只要在build.gradle中加入
compile ‘com.android.support:design:23.3.0’即可。
demo演示:
直接上代码:
直接看代码应该更好理解,说多反而乱。
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/titlebar"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width=