针对 TV 端的特性对 TabLayout 进行了适配与开发;
支持图片或颜色值作为 Indicator;
效果
Android Studio 集成
// support 版本
implementation 'com.owen:tv-tablayout:1.0.4'
// androidx 版本
implementation 'com.owen:tv-tablayout:2.0.0'
自定义属性
属性
值/类型
简介
tabIndicatorResId
reference
指示器的资源(图片或颜色)
tabIndicatorWidth
dimension
指示器的宽
tabIndicatorHeight
dimension
指示器的高
tabIndicatorGravity
top / center / bottom
指示器的位置
tabIndicatorBackgroundHeight
dimension
指示器背景高度
tabIndicatorBackgroundColor
color
指示器背景颜色
tabContentStart
dimension
内容开始距离
tabContentBottom
dimension
内容底部距离
tabCircleDotRadius
dimension
小圆点的半径
tabCircleDotColor
color / reference
小圆点的颜色
tabScrollMode
scrollable / fixed
滚动模式
tabBackground
reference
tab 背景
tabGravity
top / center / bottom / fill
tab 的位置
tabMinWidth
dimension
tab 最小宽度
tabMaxWidth
dimension
tab 最大宽度
tabTexSize
dimension
文本大小
tabTextColor
color / reference
文本颜色
tabTextSelectedScale
float
文本选中时的缩放比例
tabTextSelectedCentered
boolean
文本选中时是否居中
tabPaddingStart
dimension
tab padding start
tabPaddingTop
dimension
tab padding top
tabPaddingEnd
dimension
tab padding end
tabPaddingBottom
dimension
tab padding bottom
tabPadding
dimension
tab padding
使用
// 代码动态添加 tab
TvTabLayout mTabLayout = findViewById(R.id.tablayout);
for(int i=0; i<15; i++) {
mTabLayout.addTab(mTabLayout.newTab().setText("标题"+i), i == 0);
}
// 布局直接指定 tab
android:id="@+id/tablayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
app:tabIndicatorResId="@drawable/zx_p1_light"
app:tabIndicatorWidth="108dp"
app:tabIndicatorHeight="15dp"
app:tabIndicatorBackgroundColor="@color/color_88ff804B"
app:tabIndicatorBackgroundHeight="3dp"
app:tabMinWidth="160dp"
app:tabScrollMode="fixed"
app:tabGravity="center"
app:tabPaddingBottom="12dp"
app:tabTextSelectedScale="1.2"
app:tabTexSize="22sp"
app:tabTextColor="@drawable/selector_tab_font_color">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题 1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题 2"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题 3"/>
// 监听设置
mTabLayout.addOnTabSelectedListener(new TvTabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TvTabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TvTabLayout.Tab tab) {
}
@Override
public void onTabReselected(TvTabLayout.Tab tab) {
}
});
更详细的使用请见 exmaple
其它作品
TvRecyclerView 框架TvRecyclerView