【问题标题】:Custom TabLayout android error when add tab to mTabs将选项卡添加到 mTab​​ 时自定义 TabLayout android 错误
【发布时间】:2018-02-11 06:49:39
【问题描述】:

我制作了自定义 tablayout 视图来自定义 tablayout android 的字体,但它让我在 mTab​​s.add(position, tab); 上崩溃我的代码有什么问题?

@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

【问题讨论】:

    标签: android android-tablayout


    【解决方案1】:

    尝试在您的标签布局中设置自定义标签

    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);
    

    【讨论】:

    • 未修复错误在 mTab​​s.add(position, tab);当我评论它时。 tablayout 工作正常,但我无法更改文本和其他所有内容的颜色
    • 它不依赖于TextView,我说当我想将选项卡添加到ArrayList时会出错,ArrayList为空并且索引10超出范围
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2015-12-15
    相关资源
    最近更新 更多