前两篇文章中介绍了TabLayout和ViewPgaer+Fragment的使用,这篇文章将三者结合起来,做成一个比较常见的布局,如图:
如果对着这两者有什么不清楚的,可以看看这两篇,都是一些简单的使用。
废话不多说,直接上代码。
要使用TabLayout,必须先在app的build.gradle中添加:
compile 'com.android.support:design:23.1.1'
在主布局中添加TabLayout和ViewPager
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"