先上效果图
在使用TabLayout 之前需要导入design包。 我使用的是android studio 只要在build.gradle中加入
compile 'com.android.support:design:24.2.0' 即可。
一、首先看一下布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:andro 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context=".MainActivity"> 10 11 12 <android.support.design.widget.TabLayout 13 android: 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:background="@color/colorPrimary" 17 app:tabIndicatorColor="@color/white" 18 app:tabSelectedTextColor="@color/red" 19 app:tabTextColor="@color/white" 20 > 21 </android.support.design.widget.TabLayout > 22 23 <android.support.v4.view.ViewPager 24 android: 25 android:layout_width="match_parent" 26 android:layout_height="0dp" 27 android:layout_weight="1"> 28 29 </android.support.v4.view.ViewPager> 30 31 </LinearLayout>