0

单击tabhost时我想更改图标

下面是源代码

    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);
}

当用户按下我在 tabhost 事件下使用的选项卡时,我想更改选项卡的图标

   tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {

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

但无法成功

还有一个 tab_indicer.xml 文件,但只有背景会改变而不是图标

下面是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"/>

下面是屏幕截图

在此处输入图像描述

当我们点击任何一个标签时,它的图标应该像这里一样改变,它必须变成橙色..

有谁能够帮我...

4

1 回答 1

1

没有直接的方法可以更改 TabSpec 图标。你必须把你的图标放在选择器drawables中。

于 2013-05-15T08:58:26.827 回答