自定义TabLayout Android错误时添加选项卡mTabs
问题描述:
我让我的自定义Tablayout视图自定义Tablayout android的字体,但它给我崩溃mTabs.add(位置,选项卡);我的代码有什么问题?自定义TabLayout Android错误时添加选项卡mTabs
@Override
public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
MyTextView myTextView = (MyTextView)
View.inflate(getContext(), R.layout.tab_item_template, null);
myTextView.setText(tab.getText());
tab.setCustomView(myTextView);
mTabs.add(position, tab);
super.addTab(tab, position, setSelected);
}
误差
java.lang.IndexOutOfBoundsException: Invalid index 10, size is 0
答
试试这个设置自定义选项卡中的标签布局
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setTextColor(ContextCompat.getColor(this,R.color.colorAccent));
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);// if you want to set icon on that tab
tabLayout.getTabAt(0).setCustomView(tabOne);
未修复的错误是在mTabs.add(位置,标签);当我评论它。 tablayout工作正常,但我不能改变文本颜色和其他一切 –
@AliRanjbarzadeh检查更新ans –
它不依赖于TextView,我说当我要添加选项卡到ArrayList它使错误,ArrayList是空的和索引10出的界限 –