简体   繁体   English

更改Tabhost Android上的图标

[英]change icon on tabhost android

i want to change icon when click on tabhost 我想在点击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事件下方使用的Tab键时更改Tab键的图标

   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 还有一个tab_indicater.xml文件,但是从那以后只会更改背景而不是图标

below is the xml code 下面是xml代码

   <?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. 没有简单的方法可以更改TabSpec图标。 You have to put your icons in the selector drawables. 您必须将图标放入选择器可绘制对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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