com.google.android.material.tabs.TabLayout的属性及用法

com.google.android.material.tabs.TabLayout 是 Android Material Design 中的选项卡组件,支持丰富的自定义属性。以下是其核心属性分类说明:


📌 基础布局属性

  1. 1. app:tabMode
    控制标签页的显示模式:
    • • fixed:等宽分布(默认)
    • • scrollable:内容自适应宽度,可横向滚动
  2. 2. app:tabGravity
    设置标签对齐方式:
    • • fill:均匀填充父容器
    • • center:居中显示

🎨 样式与颜色

  1. 1. 指示器(Indicator)
    • • app:tabIndicatorColor:设置指示器颜色
    • • app:tabIndicatorHeight:调整指示器高度
    • • app:tabIndicatorFullWidth:控制指示器是否与标签等宽(默认 true
  2. 2. 文本样式
    • • app:tabTextColor:未选中标签文本颜色
    • • app:tabSelectedTextColor:选中标签文本颜色
    • • app:tabTextAppearance:引用自定义文本样式(字体、大小等)
  3. 3. 背景与涟漪效果
    • • app:tabBackground:设置标签背景(Drawable 或颜色)
    • • app:tabRippleColor:点击时的涟漪效果颜色

📏 尺寸与间距

  1. 1. 宽度控制
    • • app:tabMinWidth:标签最小宽度
    • • app:tabMaxWidth:标签最大宽度
  2. 2. 内边距
    • • app:tabPaddingStart/app:tabPaddingEnd:标签内容左右内边距
    • • app:tabPaddingTop/app:tabPaddingBottom:标签内容上下内边距(需手动设置)

⚙️ 高级功能

  1. 1. 动画与交互
    • • app:tabIndicatorAnimationDuration:指示器切换动画时长(毫秒)
    • • app:tabInlineLabel:文本与图标是否同行显示(默认 false
  2. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值