用途
作为标签栏,和V i e w P a g e r页面实现联合滑动。
添加依赖
implementation 'com.android.support:design:26.1.0'
编写xml文件
app:tabMode=“fixed” 固定标题栏;scrollable指标题栏可滑动
tabGravity="center"居中显示,fill 占满全屏
tabIndicatorColor 导航条中标题被选中时下划线的颜色
tabSelectedTextColor导航条中标题被选中时标题栏的颜色
<com.google.android.material.tabs.TabLayout android:id="@+id/record_tabs" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerHorizontal="true" app:tabGravity="center" app:tabMode="fixed" app:tabTextColor="#7D7D7D" app:tabSelectedTextColor="@color/black" app:tabIndicatorColor="@color/black"/>
<androidx.viewpager.widget.ViewPager android:id="@+id/record_vp" android:layout_width="match_parent" android:layout_height="match_parent"/>
绑定ViewPager和TabLayout
TabLayout tableLayout; ViewPager viewPager; tableLayout=findViewById(R.id.record_tabs);//查找控件 viewPager=findViewById(R.id.record_vp); tableLayout.setupWithViewPager(viewPager);//绑定