2011-09-05 226 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); 
}}); 
相關問題