2011-03-08 72 views
1

這是我用Android選項卡獲得的奇怪行爲。或者,也許模擬器是責備!在下面你有用於佈局的XML。Android Tabs問題

爲每個選項卡單獨設計的佈局是res/layout/tab1.xml和res/layout/tab2.xml ...並且一切似乎都能正常工作,除非在繪製時重疊選項卡。如果我選擇TAB1,一切看起來都很正常,但如果我選擇TAB2,它只是繪製tab1佈局。如果我回到tab1然後再一切看起來很正常。

那麼爲什麼它會從tab1中繪製佈局,而不是從黑色/空白屏幕開始?在繪製標籤2的佈局和內容之前,TabWidget是否應該處理這些情況並調用clear()方法或其他東西?

我正在模擬Android 2.2(API級別8)的機器。

<?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"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:padding="5dp"> 

     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:padding="5dp"> 

      <include layout="@layout/tab1" /> 
      <include layout="@layout/tab2" /> 

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

編輯:根據要求,我會添加TAB2佈局基本上是一個空白的屏幕!

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</LinearLayout> 

那麼,爲什麼,如果我切換到TAB2任何想法...它只是借鑑(添加字符串)在TAB1的控件(按鈕等),並沒有從空白/黑色屏幕開始?

+0

你也可以發佈tab2的佈局嗎?也許它的寬度和高度設置爲wrap_content? – Adinia 2011-03-08 10:18:33

+0

第二個選項卡只包含一個我正在寫東西的空白屏幕。所以它只包含一個線性佈局。但文本出現在第一個選項卡的按鈕上......而不是帶有一些文本的空白黑屏。雖然我會嘗試你所說的,但從wrap context改爲fill_parent可能會做一些有用的事情。 – Tibbers 2011-03-08 12:09:59

回答

0

解決了這個問題!

你並不需要包括(我剛剛刪除他們從以前的main.xml)

<include layout="@layout/tab1" /> 
<include layout="@layout/tab2" /> 

因爲它會覆蓋從兩個TAB1和TAB2的GUI元素。相反,只需使用tabwidget和framelayout即可保留main.xml。

你需要做的就是在onCreate方法的相應類中調用: setContentView(R.layout.tab1)和setContentView(R.layout.tab2)!就是這樣。這就是你如何在每個標籤上添加控件(按鈕,文本編輯等)(它本身就是一項活動)

現在它像現在的魅力一樣運作!!!!