com.google.android.material.tabs.TabLayout
是 Android Material Design 中的选项卡组件,支持丰富的自定义属性。以下是其核心属性分类说明:
📌 基础布局属性
- 1.
app:tabMode
控制标签页的显示模式:- •
fixed
:等宽分布(默认) - •
scrollable
:内容自适应宽度,可横向滚动
- •
- 2.
app:tabGravity
设置标签对齐方式:- •
fill
:均匀填充父容器 - •
center
:居中显示
- •
🎨 样式与颜色
- 1. 指示器(Indicator)
- •
app:tabIndicatorColor
:设置指示器颜色 - •
app:tabIndicatorHeight
:调整指示器高度 - •
app:tabIndicatorFullWidth
:控制指示器是否与标签等宽(默认true
)
- •
- 2. 文本样式
- •
app:tabTextColor
:未选中标签文本颜色 - •
app:tabSelectedTextColor
:选中标签文本颜色 - •
app:tabTextAppearance
:引用自定义文本样式(字体、大小等)
- •
- 3. 背景与涟漪效果
- •
app:tabBackground
:设置标签背景(Drawable 或颜色) - •
app:tabRippleColor
:点击时的涟漪效果颜色
- •
📏 尺寸与间距
- 1. 宽度控制
- •
app:tabMinWidth
:标签最小宽度 - •
app:tabMaxWidth
:标签最大宽度
- •
- 2. 内边距
- •
app:tabPaddingStart
/app:tabPaddingEnd
:标签内容左右内边距 - •
app:tabPaddingTop
/app:tabPaddingBottom
:标签内容上下内边距(需手动设置)
- •
⚙️ 高级功能
- 1. 动画与交互
- •
app:tabIndicatorAnimationDuration
:指示器切换动画时长(毫秒) - •
app:tabInlineLabel
:文本与图标是否同行显示(默认false
)
- •
- 2. 自定义标签项
通过app:tabCustomView
或代码动态添加自定义布局(如带红点的标签)
🌟 使用示例
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabIndicatorColor="@color/primary"
app:tabSelectedTextColor="@color/primary"
app:tabTextColor="@color/gray"
app:tabIndicatorHeight="4dp"
app:tabIndicatorFullWidth="false"/>
⚠️ 注意事项
- • 需添加 Material 组件依赖:
implementation 'com.google.android.material:material:1.9.0'
- • 与
ViewPager2
搭配时,建议使用TabLayoutMediator
绑定数据 - • 深度自定义需结合
TabItem
或重写TabView