Changing the icon of selected tab

喜欢而已 提交于 2020-01-24 09:50:26

问题


Im setting the tablayout as follows,

 private void setupTabIcons() {

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

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

How can I be able to change the icon of selected tab? im using tablayout.


回答1:


I am using TabLayout.OnTabSelectedListener:

wptabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab.setIcon(R.drawable.newicon);
             //also you can use tab.setCustomView() too
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            tab.setIcon(R.drawable.oldicon);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });


来源:https://stackoverflow.com/questions/37630765/changing-the-icon-of-selected-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!