2013-05-10 143 views
0

我想改变图标时点击tabhost改变图标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); 
} 

我想改变标签的图标时,该用户按tab我用下面tabhost事件

tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 

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

但不能成功

也有tab_indicater.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"/> 

以下

的屏幕截图

enter image description here

当我们点击任何一个选项卡上的图标应该是喜欢这里的变化就必须变为橙色..

任何机构可以帮我...

回答

1

有改变则tabspec图标没有直接的方法。你必须把你的图标放在选择器的drawable中。

+0

但在这也是你怎么能区分这个图标是这个标签,该图标是该标签? – 2013-05-15 09:52:36

+0

简单,为不同的标签使用不同的绘图:) – iceman 2013-05-17 04:29:19

+0

谢谢iceman :)我会试试:) – 2013-05-17 10:34:02