如何更改所选选项卡中的图标?我为选项卡中的设置图标执行此操作:
private void createTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.start_n, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
}
但是当我选择或滚动选项卡时,我想更改所选选项卡中的图标并更改为上一个选项卡中的默认图标
如果您使用的是材料设计,请尝试此操作
private int[] imageResIdcolor = {
R.drawable.ic_dashboard,
R.drawable.ic_chats,
R.drawable.ic_friends,
R.drawable.ic_contacts,
};
private int[] imageResId = {
R.drawable.ic_dashbordblue,
R.drawable.ic_chatsblue,
R.drawable.ic_friendsblue,
R.drawable.ic_contactsblue,
};
@Override
public void onTabSelected(TabLayout.Tab tab) {
tabLayout.getTabAt(tab.getPosition()).setIcon(imageResId[tab.getPosition()]);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tabLayout.getTabAt(tab.getPosition()).setIcon(imageResIdcolor[tab.getPosition()]);
}