2011-11-14 85 views
0

我試圖創建一個具有2-tabbed活動的應用程序,其中tab1上有一張圖片和一些文本,tab2有兩個按鈕用戶可以點擊(這應該導致進一步的活動)Android Tabs:第二個選項卡中的內容隱藏在第一個選項卡的內容下

由於某種原因,我無法獲得tab2上的按鈕來顯示;我只能看到與tab1相同的圖片。仔細觀察,我發現這些按鈕隱藏在tab1的圖片下。我究竟做錯了什麼?我如何讓這張照片消失,而不是顯示在tab2上?

以下是我的代碼:

IndexActivity

public class IndexActivity extends TabActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TabHost.TabSpec spec = getTabHost().newTabSpec("tab1"); 
    spec.setContent(R.id.tab1content); 
    spec.setIndicator("tab1"); 
    getTabHost().addTab(spec); 

    spec = getTabHost().newTabSpec("tab2"); 
    spec.setContent(R.id.tab2content); 
    spec.setIndicator("tab2"); 
    getTabHost().addTab(spec); 

    getTabHost().setCurrentTab(0);  

    } 
} 

我的main.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"> 
    <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">  
       <include layout="@layout/tab1"/> 
       <include layout="@layout/tab2"/>    
      </FrameLayout>   
</LinearLayout> 

tab1.xml是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <ImageView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center" 
        android:src="@drawable/pic"/> 
     <ScrollView android:id="@+id/scrolltab1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
     <TextView android:id="@+id/tab1content" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
        android:background="#65000000" 
        android:text="@string/text" 
      android:textColor="#ffffffff" 
      android:textSize="20sp" 
       android:padding="15dp" 
     /> 
     </ScrollView>  
</merge> 

TAB2是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tab2content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.10" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.10" 
     android:text="Button" /> 

</LinearLayout> 

請幫幫忙!我一定是被忽視的東西,但我想不出我什麼sugggest :(

回答

0

main.xml取下FrameLayout以下兩行,並看看會發生什麼。

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

我有點需要這兩個佈局,雖然... – nubicurio

+0

但你試過看看發生了什麼?原因是你多次添加視圖。當您調用'setContentView(R.layout.main)'時,將自動添加'tab1.xml'和'tab2.xml'的佈局,但是您將創建'TabSpecs',其中第一個包含'TextView'(tab1content )來自'tab1.xml',第二個包含'tab2.xml'(tab2content)的整個佈局。當您調用'spec.setContent(...)'時,由於main.xml中的元素,您正手動重複佈局文件內容的通貨膨脹,因此setContentView(R.layout.main)已完成。 – Squonk

+0

我拿出包含文件,應用程序根本無法啓動(強制關閉);奇怪的是,我似乎通過將android:src更改爲android:background來解決問題。但是你做了什麼很有意義......這是否意味着我實際上有4個選項卡,即使我只看到兩個選項卡?我不知道我在做什麼:( – nubicurio

0

你看看這個....

你例子其實公平地代表這個例子.... android tabhost tutorial

我希望你已經看過了....這是我用來當我弗里斯特開發了我的第一個Android標籤插件的一個...

PS:你看看這個例子中,上述的part 2 .. 。延續每一件事,你需要知道的一切。

+0

謝謝!我會在早上看看這第一件事... – nubicurio

+0

它解決了你的問題...如果是的話,你會介意我的答案作爲你的問題的答案..如果沒有...請問你能告訴我你試過的東西, – medampudi

0

看你的代碼和XML我認爲這個問題是與該行tab1.xml:

<TextView android:id="@+id/tab1content"... 

嘗試將佈局ImageViewScrollView左右。並將android:id="@+id/tab1content"移動到佈局。事情是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <LinearLayout android:id="@+id/tab1content" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <ImageView android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:scaleType="center" 
       android:src="@drawable/pic"/> 
     <ScrollView android:id="@+id/scrolltab1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
     <TextView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#65000000" 
      android:text="@string/text" 
      android:textColor="#ffffffff" 
      android:textSize="20sp" 
      android:padding="15dp" /> 
     </ScrollView> 

    </LinearLayout> 
</merge> 
+0

嘿,按鈕現在顯示在tab2上;但是,tab1中的文本「@ string/text」不再可見:( – nubicurio

+0

是的,這與佈局有所不同,您應該在此主題中標記答案並針對新問題提出一個新問題。 – C0deAttack

+0

其他的東西,但我改變了android:src到android:背景,修復了我的問題... – nubicurio

相關問題