2012-02-09 92 views
1

我已經開始在Tablayout中自定義Tabs,所以我按照這個教程http://joshclemm.com/blog/?p=136 ,它工作正常。但懷疑是我無法找到如何添加一個活動到本教程中的每個選項卡,我已經嘗試過但沒有找到解決方案。如何將活動添加到選項卡?

一般我們在使用

TabSpec spec = tabHost.newTabSpec("Photos"); 
    spec.setIndicator("Photos"); 

    Intent photos = new Intent(this, Photos.class); 
    spec.setContent(photos); 

,但不知道如何在這種情況下,添加一個活動。

有人能幫我擺脫這個嗎?

謝謝。

回答

0

你是在正確的軌道上 - Photos.class只是需要一個活動。

參見例如this TabActivity我如何與Zwitscher

+0

感謝。你有沒有看到該教程中的代碼,你能告訴我該編寫代碼的位置,我很困惑... @Heiko Rupp – Taruni 2012-02-09 10:04:55

0

做到了您只需將您的名字活動類的其他名稱替換單詞照片。 但請記住,您必須在androidmanifest文件中註冊您的活動。

+0

告訴我該代碼放在該教程代碼的位置 – Taruni 2012-02-09 10:21:00

+0

示例我創建一個名爲的Test.class 然後我打開AndroidManifest,在AndroidManifest.xml標籤,我將添加一些代碼行是這樣的: <活動 機器人:名稱= 「MainActivity。」 機器人:標籤= 「@串/ APP_NAME」> tttrung43 2012-02-15 09:53:24

1
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 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, DemoActivity1.class); 
     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("todaystake").setIndicator("Todays Take", 
       res.getDrawable(R.drawable.icontodaystake)).setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 
     intent = new Intent().setClass(this, DemoActivity2.class); 
     spec = tabHost.newTabSpec("whatscasting").setIndicator(
       "What's Casting", res.getDrawable(R.drawable.iconwhatscasting)) 
       .setContent(intent); 
     tabHost.addTab(spec); 
tabHost.setCurrentTab(0); 

您的XML文件將是這個樣子

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <RelativeLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:layout_above="@android:id/tabs" /> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_alignParentBottom="true" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 
</TabHost> 
+0

請告訴我,在源代碼中寫入的位置... – Taruni 2012-02-09 10:28:44

+0

在您的主類中添加此選項,您在xml中添加選項卡主機 – 2012-02-09 10:47:02

+0

感謝您的幫助,但請檢查我在問題中提到的鏈接並提供答案。 – Taruni 2012-02-09 11:18:48

相關問題