1、创建自定义View
public View getTabView() {
View tabView = LayoutInflater.from(mContext).inflate(R.layout.tab_red_dot_layout, null);
//设置相关显示 tabView
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.width = 100;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
tabView.setLayoutParams(params);
return tabView;
}
View tabView = LayoutInflater.from(mContext).inflate(R.layout.tab_red_dot_layout, null);
//设置相关显示 tabView
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.width = 100;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
tabView.setLayoutParams(params);
return tabView;
}
2、设置给TabLayout
tablayout.getTabAt(0).setCustomView(getTabView());
3、滑动、点击事件选项卡需要自己处理
//添加tabLayout选中监听
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//设置选中时的文字颜色
if (tab.getCustomView() != null) {
TextView tabTitle = tab.getCustomView().findViewById(R.id.tv_tab_title);
tabTitle.setTextColor(getResources().getColor(R.color.colorAccent));
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//设置未选中时的文字颜色
if (tab.getCustomView() != null) {
TextView tabTitle = tab.getCustomView().findViewById(R.id.tv_tab_title);
tabTitle.setTextColor(getResources().getColor(R.color.colorTitle));
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//设置选中时的文字颜色
if (tab.getCustomView() != null) {
TextView tabTitle = tab.getCustomView().findViewById(R.id.tv_tab_title);
tabTitle.setTextColor(getResources().getColor(R.color.colorAccent));
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//设置未选中时的文字颜色
if (tab.getCustomView() != null) {
TextView tabTitle = tab.getCustomView().findViewById(R.id.tv_tab_title);
tabTitle.setTextColor(getResources().getColor(R.color.colorTitle));
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});