2017-09-02 99 views
-3

我讓我的自定義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 

回答

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

未修復的錯誤是在mTabs.add(位置,標籤);當我評論它。 tablayout工作正常,但我不能改變文本顏色和其他一切 –

+0

@AliRanjbarzadeh檢查更新ans –

+0

它不依賴於TextView,我說當我要添加選項卡到ArrayList它使錯誤,ArrayList是空的和索引10出的界限 –

相關問題