View - TabLayout

799 阅读2分钟

简介

TabLayout 是由 material design 包提供的页面切换指示器。

TabLayout 可以通过创建 TabLayout.Tab 实例添加选项卡。示例如下:

TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

TabLayout.Tab 实例可以分别通过 setTextsetIcon 更改选项卡的标签或图标。

TabLayout + ViewPager

如果 ViewPagerTabLayout 合用,可以通过 setupWithViewPager(ViewPager) 关联两者,此时 TabLayout 将使用 Pager Title 填充选项卡。ViewPager + TabLayout 示例如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
class MyFragmentPagerAdapter(private val context: Context, fragmentManager: FragmentManager, private val pagerFragments: List<Fragment>): FragmentPagerAdapter(fragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
    override fun getItem(position: Int): Fragment {
        return pagerFragments[position]
    }

    override fun getCount(): Int {
        return pagerFragments.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return String.format(context.resources.getString(R.string.appbar_sample_format), position)
    }
}
viewPager.adapter = MyFragmentPagerAdapter(this, supportFragmentManager, pagerFragmengs)
tabLayout.setupWithViewPager(viewPager)

事件监听

TabLayout 添加 OnTabSelectedListener 监听选项卡事件,OnTabSelectedListener 详情如下:

/**
 * 选项卡被选中时回调
 */
public void onTabSelected(T tab);

/**
 * 选项卡取消选中回调
 */
public void onTabUnselected(T tab);

/**
 * 当已被选中的选项卡再次选中时回调
 */
public void onTabReselected(T tab);

TabLayout xml 属性

属性简介
tabIndicatorColor指示器颜色color
tabIndicatorHeight指示器高度dp\sp
tabIndicatorFullWidth指示器宽度是否为选项卡宽度boolean
tabBackground选项卡背景color\drawable
tabMode选项卡在布局中的行为模式scrollable 可滑动; fixed 占满屏幕宽度; auto 自适应
tabInlineLabel文字和Icon 是否在水平线上boolean
tabMinWidth选项卡最小宽度dp\sp
tabMaxWidth最大宽度dp\sp
tabTextAppearance文字样式style
tabTextColor文字颜色color
tabSelectedTextColor文字选中颜色color
tabIconTintIcon 颜色混合color
tabIconTintMode颜色混合模式
tabRippleColor点按水波纹效果颜色color
tabUnboundedRipple是否使用无边界水波纹效果boolean

自定义 Tab

TabLayout 是由 Tab 组成的,可以通过 getTabAt 方法获取指定的 Tab,通过 setIconsetText 修改图标和文字。也可以使用自定义的布局替代 Tab 布局,使用 setCustomView 方法修改 Tab 的布局。

自定义 TabLayout 布局示例如下:

val view = LayoutInflater.from(this).inflate(R.layout.item_tab_layout, null)
val ivTabIcon = view.findViewById<ImageView>(R.id.ivTabIcon)
val tvTabContent = view.findViewById<TextView>(R.id.tvTabContent)
ivTabIcon.setImageResource(tabIcons[i])
tvTabContent.text = pageTitles[i]
tabLayout.getTabAt(i)?.customView = view