2011-07-10 33 views
1

我想給我的TabActivity一個自定義標題。但以下代碼不起作用。守則中有什麼錯誤?我如何爲TabActivity製作自定義標題?謝謝!Android:如何爲TabActivity製作自定義標題

public class ShowExam_TabAct extends TabActivity { 
public static boolean customTitleSupported = false; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabs); 

    customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    if (customTitleSupported) { 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
        R.layout.title); 
     TextView title = (TextView) findViewById(R.id.TV_title); 
     title.setText("new title!"); 
    } 

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 

    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Mars") 
      .setContent(new Intent(this, List1.class))); 
    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Earth") 
      .setContent(new Intent(this, List2.class))); 

} 
} 
+0

什麼是錯誤出來? – kinghomer

回答

0

嘗試以這種方式

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 

     setContentView(R.layout.main); 

     if(customTitleSupported){ 
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 
     } 

     TextView title = (TextView) findViewById(R.id.TV_title); 
     title.setText("new title!"); 
     .... 
} 
相關問題