###什麼是TabLayout
上圖中,我們可以看到,該頁面分成三個頁籤,每個頁籤對應不同的內容,如果讓我們來實現佈局的話,我們會很容易地想到佈局爲一個頁籤指示器+ViewPager,以前,相信各位大多使用的是GitHub上的開源框架PagerSlidingTabTrip來實現指示器的效果,而如今,Android中也有自帶這種指示器的控件TabLayout,TabLayout存在於android design庫中,它提供了一個水平的佈局來展示Tabs。
一 如何使用TabLayout
1.要使用TabLayout,首先要添加 android support design包的依賴,在app模塊的build.gradle中添加:
dependencies {
...
compile 'com.android.support:design:26.0.0-alpha1'
}
2.在佈局文件的xml中,使用TabLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"