官方文档
https://developer.android.google.cn/reference/com/google/android/material/tabs/package-summary
TabLayout 提供了一个水平布局来显示选项卡。
TabItem 是一个特殊的“视图”,它允许您在布局中声明选项卡项。这个视图实际上并没有添加到 TabLayout,它只是一个允许设置选项卡项的文本、图标和自定义布局的虚拟视图。
TabLayout 提供了一个水平布局来显示选项卡。
布局文件
<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"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项卡1" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项卡2" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项卡3"
/>
com.google.android.material.tabs.TabLayout>
LinearLayout>
运行
定义四个布局文件
layout1.xml
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View1"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
androidx.constraintlayout.widget.ConstraintLayout>
其他三个雷同
主布局文件
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" />
androidx.viewpager.widget.ViewPager>
androidx.constraintlayout.widget.ConstraintLayout>
适配器
MyAdapter.java
package com.dingjiaxiong.mytablayout;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class MyAdapter extends PagerAdapter {
private List<View> listView;
private List<String> listTabTitle;
private Context context;
public MyAdapter(List<View> listView, List<String> listTabTitle, Context context) {
this.listView = listView;
this.listTabTitle = listTabTitle;
this.context = context;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return listTabTitle.get(position);
}
@Override
public int getCount() {
return listView.size();
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(listView.get(position),0);
return listView.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(listView.get(position));
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
调用代码
MainActivity.java
package com.dingjiaxiong.mytablayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tab_layout);
LayoutInflater layoutInflater = getLayoutInflater();
View view1 = layoutInflater.inflate(R.layout.layout1, null);
View view2 = layoutInflater.inflate(R.layout.layout2, null);
View view3 = layoutInflater.inflate(R.layout.layout3, null);
View view4 = layoutInflater.inflate(R.layout.layout4, null);
List<View> viewList = new ArrayList<>();
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
List<String> listTabTitle = new ArrayList<>();
listTabTitle.add("选项卡1");
listTabTitle.add("选项卡2");
listTabTitle.add("选项卡3");
listTabTitle.add("选项卡4");
MyAdapter myAdapter = new MyAdapter(viewList,listTabTitle,this);
viewpager.setAdapter(myAdapter);
}
}
运行