2016-07-18 2 views
0

Поскольку этот код дубликата, я решил изменить егоИспользование массива для упрощения кода

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
tabOne.setText("ONE"); 
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0); 
tabLayout.getTabAt(0).setCustomView(tabOne); 

TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
tabTwo.setText("TWO"); 
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_call, 0, 0); 
tabLayout.getTabAt(1).setCustomView(tabTwo); 

TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
tabThree.setText("THREE"); 
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_contacts, 0, 0); 
tabLayout.getTabAt(2).setCustomView(tabThree); 

В:

private void SetupTab() { 
    TextView[] Tab = new TextView[3]; 
    int[] tabIcons = { 
     R.drawable.tab1, 
     R.drawable.tab2, 
     R.drawable.tab3 
    }; 
    String[] tabTitle = {"ONE","TWO","THREE"}; 

    for (int i = 0; i < 3; i++) { 
     TextView Tab[i] = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
     Tab[i].setText(tabTitle[i]); 
     Tab[i].setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[i], 0, 0); 
     tabLayout.getTabAt(i).setCustomView(Tab[i]); 
    } 
} 

Но я получил проблему, что моя программа не работает. Может ли кто-нибудь помочь мне решить эту проблему? Я очень благодарен вам за помощь.

+2

Ваши вводимого коэффициента не то же самое –

+1

Что вы имеете в виду вашу программу не работает? Вызывается метод SetupTab? –

+0

Извините, по моей вине я ошибся. Спасибо, что рассказали мне об этом. – helloworld

ответ

0

Я думаю, что TextView[] Tab ненужно:

private void SetupTab() { 
    int[] tabIcons = { 
      R.drawable.tab1, 
      R.drawable.tab2, 
      R.drawable.tab3 
    }; 
    String[] tabTitle = {"ONE","TWO","THREE"}; 

    for (int i = 0; i < 3; i++) { 
     TextView view = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
     view.setText(tabTitle[i]); 
     view.setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[i], 0, 0); 
     tabLayout.getTabAt(i).setCustomView(view); 
    } 
} 
+0

это лучшее решение: D Большое спасибо: D – helloworld

Смежные вопросы