```
<!--
app:tabIndicator="@drawable/tab_indicator"
app:tabIndicatorColor="#F23F4B"
直接在drawable写颜色,没效果
tabRippleColor 水波纹
-->
```
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="38dp"
app:tabIndicator="@drawable/tab_indicator"
app:tabIndicatorColor="#F23F4B"
app:tabMode="fixed"
app:tabRippleColor="@null"
app:tabSelectedTextColor="#333333"
app:tabTextAppearance="@style/MyTabTextAppearance"
app:tabTextColor="#888888" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
```
<style name="MyTabTextAppearance">
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
</style>
```
```
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item android:gravity="center">
<shape>
<size
android:width="38dp"
android:height="4dp" />
<solid android:color="#F23F4B" />
</shape>
</item>
</layer-list>
```
```
val adapter = FragmentLazyStateAdapter(this, fragments)
binding.vp2.adapter = adapter
TabLayoutMediator(binding.tabLayout, binding.vp2) { tab, position ->
tab.text = titles[position]
}.attach()
```
```
class FragmentLazyStateAdapter(
fragmentActivity: FragmentActivity,
private val fragments: MutableList<Fragment>
) :
FragmentStateAdapter(fragmentActivity) {
override fun getItemCount() = fragments.size
override fun createFragment(position: Int) = fragments[position]
}
```
```