向mTabs添加选项卡时自定义TabLayout android错误

时间:2017-09-02 11:17:34

标签: android android-tablayout

我使用自定义tablayout视图来自定义tablayout android的字体,但它让我在mTabs.add(位置,标签)上崩溃;我的代码出了什么问题?

@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

1 个答案:

答案 0 :(得分: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);