tablayout 自定义背景 添加 选中后移动到所选位置 滑动时 视图不跟随

275 阅读1分钟
//自定义tab 布局
private View setCustomView(String tabText) {
     //引入自定样式布局
    View view = View.inflate(context, R.layout.tab_icon_item, null);
    AppCompatTextView tv = (AppCompatTextView) view.findViewById(R.id.item_text_view);
    tv.setText(tabText);
    return view;
}

tabLayout.addTab(tabLayout.newTab().setCustomView(setCustomView("")));


//解决tablayout滚动 选中位置不跟随显示视图展示
tabLayout.getTabAt(tabLayout.getTabAt(position)).select();
tabLayout.post(new Runnable() {
    @Override
    public void run() {
        //position 选中的tab下标
        tabLayout.getTabAt(position).select();
    }
})

动态改变tablayout TabMode 固定模式 还是滚动模式
**```
if (tabLayout.getTabCount() > 3) {
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
} else {
    tabLayout.setTabMode(MODE_FIXED);
}
```**