TabLayout自定义间距
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_gravity="center"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginHorizontal="@dimen/dp_44"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:tabBackground="@android:color/transparent"
app:tabGravity="center"
app:tabIndicator="@drawable/shape_tab_indicator_me"
app:tabIndicatorColor="@color/color_00CD83"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:tabRippleColor="@android:color/transparent" />
主要添加属性
app:tabMaxWidth="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
创建tab View
for (index in titles.indices) {
val newTab = binding.tabLayout.newTab()
val tabView = ItemWalletTitleTabBinding.inflate(layoutInflater).run {
tabBindings.add(this)
if (index == 0) {
this.ivTab.alpha = 1f
} else {
this.ivTab.alpha = 0.4f
}
this.ivTab.setImageResource(titles[index])
layout
}
binding.tabLayout.addTab(newTab.setCustomView(tabView))
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivTab"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginHorizontal="@dimen/dp_6"
android:scaleType="fitCenter" />
</FrameLayout>
</layout>