8.TabLayout简介
public class TabLayout extends HorizontalScrollView {
所有的 Tab 选项卡实例化都是通过 TabLayout.Tab 完成的。你可以通过 TabLayout.newTab()来创建 Tab 对象。你可以通过更改Tab 的setText()、setIcon()分别设置选项卡的文字和 Icon。要显示选项卡 Tab,你必须通过一个方法 addTab(tab)方法将其添加到布局。例如:
如果你的 ViewPager 和这个布局用在一起,你可以调用 setupWithVIewPager(ViewPager)两个链接在一起,这种布局将会自动填充 PagerAdapter 的页面标题
你也可以把这种用法当成 ViewPager 的装饰,并且可以这样写布局资源直接添加到 ViewPager 当中:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
- app:tabSelectedTextColor:选中字体的颜色
- app:tabTextColor:未选中字体的颜色
- app:tabIndicatorColor:指示器下标的颜色
- app:tabIndicatorHeight:指示器下标的高度
- app:tabGravity:tab中布局位置,有两种模式center和fill
- app:tabTextAppearance:字体大小
- app:tabBackground:设置背景
- app:tabMode:设置tablayout的排列模式,有两种scrollable和fixed;默认是fixed。fixed是固定的,适用于标签较少,scrollable是可滚动的,适用于标签较多的情况下