android之TabLayout介绍

 2026-08-11 16:57:33    2089  

image.png

TabLayoutTabLayout是Android support中的一个控件android.support.design.widget.TabLayout,Google在升级了AndroidX之后,将TabLayout迁移到material包下面去了com.google.android.material.tabs.TabLayout,原来的support下面的TabLayout从API 29开始就不再维护了。

所以如果项目已经升级了AndroidX,建议直接使用后者。

TabLayout一般结合ViewPager+Fragment的使用实现滑动的标签选择器。

实战activity_main.xml:

代码语言:javascript复制

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">

android:id="@+id/tab_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="667dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.100000024"/>

android:id="@+id/view_pager"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:layout_constraintTop_toBottomOf="@+id/tab_layout" />

MainActivity.java:

代码语言:javascript复制package com.exmple.tabloayout;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentPagerAdapter;

import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private String[] tabs = {"tab1", "tab2", "tab3"};

private List tabFragmentList = new ArrayList<>();

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TabLayout tabLayout = findViewById(R.id.tab_layout);

ViewPager viewPager = findViewById(R.id.view_pager);

//添加tab

for (int i = 0; i < tabs.length; i++) {

tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));

tabFragmentList.add(TabFragment.newInstance(tabs[i]));

}

viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

@NonNull

@Override

public Fragment getItem(int position) {

return tabFragmentList.get(position);

}

@Override

public int getCount() {

return tabFragmentList.size();

}

@Nullable

@Override

public CharSequence getPageTitle(int position) {

return tabs[position];

}

});

//设置TabLayout和ViewPager联动

tabLayout.setupWithViewPager(viewPager,false);

}

}fragment_tab.xml

代码语言:javascript复制

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/tv_bg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView" />

TabFragment.java:

代码语言:javascript复制package com.exmple.tabloayout;

import android.graphics.Color;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

public class TabFragment extends Fragment {

public static TabFragment newInstance(String label) {

Bundle args = new Bundle();

args.putString("label", label);

TabFragment fragment = new TabFragment();

fragment.setArguments(args);

return fragment;

}

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_tab, container, false);

}

@Override

public void onStart() {

super.onStart();

String label = getArguments().getString("label");

TextView text = getView().findViewById(R.id.tv_bg);

text.setText(label);

text.setBackgroundColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));

}

}执行结果:

image.png


俗话说:“脚踏一星掌千军,脚踩七星管天下”,什么是脚踩七星?
近90%中国人讨厌日本?真相背后的复杂情感与现实