使用 TabLayout 很容易实现选项卡的功能。这里将结合 ViewPager、Fragment、TabLayout实现一个具有选项卡功能的程序。本例再上篇讲解的ViewPager与Fragment实例上修改,请结合上篇实例参考。
要实现这样一个程序,首先需要在布局文件中加入 TabLayout。MainActivity中对应的布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab"
app:tabMode="fixed"
android:layout_width="match_parent"
android