2011-09-28 156 views
0

我TabActivity 與它是由幾個不同的活動,這樣的底部標籤:如何更改標籤的標題TabActivity

public class HomeScreen extends TabActivity { 
private TabHost mTabHost; 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    try{ 
    setContentView(R.layout.hometabs); 


    Resources res = getResources(); // Resource object to get Drawables 
    mTabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Reusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 
    String strName; 
    // Create an Intent to launch an Activity for the tab (to be reused) 
    strName=Central.Res.getString(R.string.tab_Books); 
    intent = new Intent().setClass(this, BookList.class); 
    spec = mTabHost.newTabSpec(strName).setIndicator("", 
         res.getDrawable(R.drawable.ic_menu_database)) 
        .setContent(intent); 
    mTabHost.addTab(spec); 


    // Create an Intent to launch an Activity for the tab (to be reused) 
    strName=Central.Res.getString(R.string.tab_Questions); 
    intent = new Intent().setClass(this, QuestionsList.class); 
    intent.putExtra("NoteType", UserDataSQLHelper.NOTE_TYPE_QUEST); 
    spec = mTabHost.newTabSpec(strName).setIndicator("", 
         res.getDrawable(R.drawable.ic_menu_dialog)) 
        .setContent(intent); 
    mTabHost.addTab(spec); 

我的問題是,我對每一個相同的窗口標題標籤。此窗口標題設置爲應用程序名稱。

我需要能夠將此標題更改爲選項卡的名稱。

我試圖通過調用setTitle(「MyTitle」);在相應的活動中的onCreate(),並通過覆蓋TabActivity onChildTitleChanged:

 public void onChildTitleChanged(Activity childActivity, CharSequence title) 
     { 
      View tabView = mTabHost.getCurrentTabView(); 
      int idTitle = android.R.id.title; 
      TextView tvTitle = (TextView) tabView.findViewById(idTitle); 
      tvTitle.setText(title); 
     } 

但結果我得到的是不是我所需要的 - 它的標籤圖標下設置文本標籤。

你能幫我嗎?

回答

6

嘗試用下面的代碼,

tabHost.setOnTabChangedListener(new OnTabChangeListener() {   
      @Override 
      public void onTabChanged(String tabId) {     
       Log.i("Tab id", ""+tabId); 
       setTitle(tabId); 
      } 
     });  
+0

謝謝,已經試過了。相同 - 更改標籤圖標下方的文字。 – Ivan

+0

嗨,現在編輯我的答案。試試我上面發佈的新代碼。 –

+0

謝謝!有用。唯一的事情是,第一次打開的標籤仍然會得到錯誤的標題,用戶必須切換標籤才能重置它。 – Ivan

0

setIndicator (CharSequence label, Drawable icon)
使用此功能,標籤將顯示爲標題。