Updating Android Tab Icons

前端 未结 5 1130
广开言路
广开言路 2020-12-08 04:26

I have an activity which has a TabHost containing a set of TabSpecs each with a listview containing the items to be displayed by the tab. When each TabSpec is created, I set

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 04:53

    Just to confirm dominics answer, here's his solution in code (that actually works):

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        public void onTabChanged(String tabId) {
            if (TAB_MAP.equals(tabId)) {
                ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
                iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
            } else if (TAB_LIST.equals(tabId)) {
                ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
                iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
            }
        }
    });
    

    Of course it's not polished at all and using those direct indices in getChildAt() is not nice at all...

提交回复
热议问题