2011-04-12 153 views
0

我試圖獲得一個在Android上工作的選項卡式佈局。使用一個例子,我設法讓標籤工作,但我的不同標籤在不同的活動。每個標籤都是相似的,它幾乎相同,所以我希望它們在同一活動中。選項卡中的XML佈局android(tabhost)

我想要一個佈局文件夾中的xml文件作爲內容。

這個工作,獨立班:

tabSpec1.setIndicator("tabName1").setContent(new Intent(this, FirstTab.class)); 

這不起作用:

tabSpec2.setIndicator("tabName2").setContent(R.layout.tab); 

的setContent函數接受ID就是這樣。標籤的引用也在我的R類中。這是在我的選項卡中的XML:

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="This is the tab from XML"/> </LinearLayout> 
+0

嗨,你能澄清你的意思嗎?「每個標籤都是相似的,它幾乎相同,所以我希望它們在同一活動中。」 你的意思是你想要在每個標籤中同一個活動的多個實例嗎? – Cephron 2011-04-12 18:02:09

+0

另外,你的意思是什麼不起作用?你收到錯誤信息還是顯示空白? – James 2011-04-12 18:16:15

+0

@ Cephron我只想將XML佈局放入選項卡中。有幾個原因,但我認爲它並不重要 – 2011-04-12 18:18:00

回答

0

有幾種方法可以做到這一點。一種方法是添加一個像Cephron在另一個答案中顯示的視圖。

取決於選項卡活動的相似程度。另一種做法是將意圖傳遞給具有不同額外功能的相同活動,然後在活動代碼中解析它們。

喜歡的東西:

Intent intent = new Intent(this, FirstTab.class); 
intent.putExtra("callingTab", 1); 
tabSpec1.setIndicator("tabName1").setContent(); 

您的活動代碼可以使用這樣的:

if (FirstTab.this.getIntent().getExtras() != null) { 
     callingTab = this.getIntent().getExtras().getInt("callingTab"); 
} 

這將允許您使用相同的佈局兩個活動,你需要編碼的細微變化英寸

1

檢查:unable to add views in TabHost by IDs, crashing

如果你想查看IDS設置每個選項卡的內容,你必須確保有一些佈局XML定義TabHostTabWidgetFrameLayout標籤正確。然後,您想要通過ID設置的視圖可以作爲子標籤放在FrameLayout中。如果沒有上述標籤中的任何一個,應用程序將因「找不到視圖ID xxxxxx」而崩潰。

你可以通過結合這個和<include>標籤來得到你想要的。

0

我覺得你需要的是在每個標籤中都有視圖而不是活動。嘗試這樣的事情......

在該選項卡活動:

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab_page_layout); 

    //Setting up TabWidget 
    Resources res = getResources(); 
    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec; 
    spec = tabHost.newTabSpec("firstTab") 
      .setIndicator(res.getString(R.string.tab1_label), res.getDrawable(R.drawable.tab1_icon)) 
      .setContent(R.id.tab1_layout); 
    tabHost.addTab(spec); 
    spec = tabHost.newTabSpec("secondTab") 
      .setIndicator(res.getString(R.string.tab2_label), res.getDrawable(R.drawable.tab2_icon)) 
      .setContent(R.id.tab2_layout); 
    tabHost.addTab(spec); 
    tabHost.setCurrentTab(0); 
} 

在tab_page_layout.xml文件:

<FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout android:id="@+id/tab1_layout" 
     /> 
     <!--your stuff--> 
     </LinearLayout> 
     <LinearLayout android:id="@+id/tab2_layout" 
     /> 
     <!--your stuff--> 
     </LinearLayout> 
    </FrameLayout> 

(以上只是一個片段,當然你」。我仍然需要包含TabWidget並使用相應的標識)

+0

在你有「<! - 你的東西 - >」的地方,有沒有辦法指定一個佈局xml?我做了我的佈局作爲單獨的XML佈局,現在我只想將它們粘貼到選項卡中。 – James 2016-10-03 21:05:16