简体   繁体   中英

change icon on tabhost android

i want to change icon when click on tabhost

below is the source code

    private void setTabs() {
    addTab("Home", TabHome.class, R.drawable.home);
    addTab("Performers", TabPerformers.class, R.drawable.performers);
    addTab("Tickets", TabTickets.class, R.drawable.tickets);
    addTab("Info", TabInfo.class, R.drawable.info);

}

private void addTab(String labelId, Class<?> c, int drawableId) {
    tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

    tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);
    spec.setIndicator(tabIndicator);
    spec.setContent(intent);

    tabHost.addTab(spec);
}

i want to change tab's icon when user press tab for that i have used below tabhost event

   tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {

            if (tabId.equals("tabHome")) {

but could not success

there is also a tab_indicater.xml file but from that only background will be change not icon

below is the xml code

   <?xml version="1.0" encoding="utf-8"?>

<!-- Non focused states -->
<item android:drawable="@drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/tab_bg_selector" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

<!-- Focused states -->
<item android:drawable="@drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

<!-- Pressed -->
<item android:drawable="@drawable/tab_bg_selector" android:state_pressed="true" android:state_selected="true"/>
<item android:drawable="@drawable/tab_press" android:state_pressed="true"/>

below is the screen shot

在此处输入图片说明

when we click on any one tab it's icon should be change like here it must become orange..

can any body help me...

There's no straightforward way to change the TabSpec icon. You have to put your icons in the selector drawables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM