<FrameLayout
android:layout_marginTop="30dp"
android:layout_marginStart="44dp"
android:layout_marginEnd="45dp"
android:background="@drawable/corner_tablayout_gray_bg"
android:layout_width="match_parent"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_height="wrap_content">
<TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="37dp"
app:tabIndicator="@drawable/corner_tablayout_bg"
app:tabIndicatorColor="@color/white"
app:tabIndicatorGravity="center"
app:tabIndicatorHeight="29dp"
style="@style/TabLayoutV2" />
FrameLayout>
<style name="TabLayoutV2">
- "tabIndicatorFullWidth"
>true
- "tabMinWidth"
>8dp
- "tabGravity">fill
- "android:layout_gravity">center
- "tabMode">fixed
- "tabTextAppearance">@style/MyFontStyle
- "tabTextColor">@color/gray
- "tabSelectedTextColor">@color/red
- "android:backgroundTint">@android:color/transparent
- "tabIndicatorColor">@color/red
- "tabRippleColor">@android:color/transparent
style>
<style name="MyFontStyle">
- "android:textColor"
>@color/color_black
- "android:lineSpacingMultiplier"
>1.2
style>
corner_tablayout_gray_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners
android:radius="30dp"/>
<solid android:color="#efefef"/>
shape>
item>
selector>
corner_tablayout_bg.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="40dp" />
<solid android:color="@color/white" />
shape>
item>
layer-list>
通过如上设置配置实现的TabLayout。关键点:
app:tabIndicator="@drawable/corner_tablayout_bg"
app:tabIndicatorColor="@color/white"
app:tabIndicatorFullWidth="true" 全局充满
app:tabIndicatorGravity="center"
app:tabIndicator="@drawable/corner_tablayout_bg"
这个是指示器图片,所以还是有平滑的滑动效果的,如果用其他方式则是瞬间移动不够美观
可以达成如下效果。