2011-09-05 179 views
0

我需要在不同状态下将不同图像设置为选项卡背景。我设置了一个图像作为默认背景,但如何在选择标签时切换到其他图像。以下是我的代码。选项卡选择背景更改

public class HelloTabWidget extends TabActivity {    

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Resources res = getResources(); // Resource object to get Drawables  
     TabHost tabHost = getTabHost(); // The activity TabHost  
     TabHost.TabSpec spec; // Resusable TabSpec for each tab  
     Intent intent; // Reusable Intent for each tab 
     TabWidget tw = getTabWidget(); 


     for (int i = 0; i < tw.getChildCount(); i++) { 
        View v = tw.getChildAt(i); 
        v.setBackgroundDrawable(getResources().getDrawable 
     (R.drawable.tab_artist)); 
        } 



     //First tab 
     intent = new Intent().setClass(this, FirstActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost  
     spec = tabHost.newTabSpec("First").setIndicator("First") 
     .setContent(intent);  
     tabHost.addTab(spec); 
     getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected); 

     //Second tab 
     intent = new Intent().setClass(this, SecondActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost  
      spec = tabHost.newTabSpec("Second").setIndicator("Second") 
      .setContent(intent);  
      tabHost.addTab(spec); 
      getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected); 

      //third 
      intent = new Intent().setClass(this, ThirdActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost  
       spec = tabHost.newTabSpec("Third").setIndicator("Third") 
       .setContent(intent);  
       tabHost.addTab(spec); 
       getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected); 

    } 
} 

/*tab_artist.xml*/

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
<!-- When selected, use grey -->  
<item android:background="@drawable/tabselected" android:state_selected="true" />  
<!-- When not selected, use white-->  
<item android:background="@drawable/tabunselected" /> 
</selector> 

回答

0
public class HelloTabWidget extends TabActivity implements OnTabChangeListener{ 
..... 

mTabHost. setOnTabChangedListener(this); 

@Override 
    public void onTabChanged(String tabId) { 

       // Here in tabId you will get the name of the Tab from that you can check and set the background 
       // of the requirement tab according to need. 
    } 
} 
0

实施onTabChangeListener()有修改他们的背景。干杯

http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html

编辑: 使用tabHost实现方法。你可以在任何你想要的地方实施。比方说,在设置所有TabWidgets之后执行此操作。它很好的做法,使用标签的ID,如您已设置他们的“第一”,“第二”等等等等

tabHost.setOnTabChangedListener(new OnTabChangeListener(){ 
      @Override 
      public void onTabChanged(String tabId) { 
         if(tabId.equals("First"){ 
           //do something 
         }else if(tabId.equals("Second")) 
         { 
          //do something 
         }// etc etc etc 


         }}); 
+0

u能PLZ告诉我我在哪里以及如何使用它的一些例子 – neha

+0

谢谢它现在的工作 – neha

+0

然后接受答案。这就是这个社区的作品:)干杯。我很高兴它的工作。 –

0

这可能会帮助你

Tabhost.setOnTabChangedListener(new OnTabChangeListener(){ 
@Override 
     public void onTabChanged(String tabId) { 

      for(int i=0;i<tb.getTabWidget().getChildCount();i++) 
      { 
        tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected 
      } 
      tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected); 
}}); 
相关问题