前言:TabLayout用来实现Tab导航栏,通常可以配合ViewPager实现滑动导航,例如网易新闻客户端采用的就是这种方式......
接下来实现的效果是这样的:
1.新建工程并添加依赖:
compile 'com.android.support:design:23.4.0'
2.修改activity_main.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.tablayout.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/id_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titleBlue"
app:tabIndicatorColor="@color/red"
app:tabSelectedTextColor="@color/chocolate"
app:tabTextColor="@color/white"/>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="matc