实现效果:tablayout个数为2,并且可以设置导航栏的宽度(tablayout没有这个属性,这里使用XTablayout)
导入依赖:
compile 'com.androidkun:XTabLayout:1.1.3'
布局中:
<com.androidkun.xtablayout.XTabLayout
android:id="@+id/xTablayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:xTabMode="scrollable"
app:xTabIndicatorColor="#03A9F4"
app:xTabIndicatorHeight="5dp"
app:xTabIndicatorWidth="100dp"
app:xTabSelectedTextColor="#03A9F4"
app:xTabTextColor="#555555"
app:xTabTextSize="21sp"
app:xTabMinWidth="240dp"
app:xTabMaxWidth="480dp"
/>
属性对应的意思:
导航栏的颜色
app:xTabIndicatorColor="#03A9F4"
导航栏的高度:
app:xTabIndicatorHeight="5dp"
导航栏的宽度:
app:xTabIndicatorWidth="100dp"
选中时文字的颜色:
app:xTabSelectedTextColor="#03A9F4"
最小的宽度:
app:xTabMinWidth="240dp"
最大的宽度:
app:xTabMaxWidth="480dp"
代码中找到id后,设置和viewpager联动即可
//设置viewpager和Xtablayout联动
xTablayout.setupWithViewPager(viewPager);