2011-01-06 52 views
0

嗨我遵循由谷歌名稱提供的教程hello-tabwidget。 創建標籤菜單。 一切工作正常,但現在我想添加一個按鈕到一個選項卡,但 此按鈕出現在所有選項卡中。使用選項卡android中的佈局 - 添加到一個選項卡的按鈕出現在所有選項卡

請任何人都可以幫忙嗎?

感謝

這是我

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

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, FirstTab.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("First Tab").setIndicator("First Tab", 
        res.getDrawable(R.drawable.ic_tab_artists)) 
       .setContent(intent); 
tabHost.addTab(spec); 

// Do the same for the other tabs 
intent = new Intent().setClass(this, SecondTab.class); 
spec = tabHost.newTabSpec("Second Tab").setIndicator("Second Tab", 
        res.getDrawable(R.drawable.ic_tab_albums)) 
       .setContent(intent); 
tabHost.addTab(spec); 

intent = new Intent().setClass(this, ThirdTab.class); 
spec = tabHost.newTabSpec("Third Tab").setIndicator("Third Tab", 
        res.getDrawable(R.drawable.ic_tab_songs)) 
       .setContent(intent); 
tabHost.addTab(spec); 

intent = new Intent().setClass(this, NextTab.class); 
spec = tabHost.newTabSpec("Next Tab").setIndicator("Next Tab", 
        res.getDrawable(R.drawable.ic_tab_next)) 
       .setContent(intent); 
tabHost.addTab(spec); 

tabHost.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"> 

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

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

    </FrameLayout> 

    <TabWidget android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom"/> 

</LinearLayout> 
</TabHost> 

我創建的XML佈局爲每這是一個與按鈕 其他標籤是完全此相同只是用一個按鈕標籤和不同的ID

tab2.xml

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

<Button android:text="@+id/Button01" 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</Button> 
</LinearLayout> 

和我創建類此每個標籤是從第二個標籤的代碼,其中i希望有一個按鈕 另一類是正是這種相同只是

setContentView(R.layout.tab2); 

被設置爲指向不同的佈局

SecondTab.java

public class SecondTab extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.tab2); 


} 
} 

的你的想法?

謝謝

回答

1

解決了!!!

main.xml中我包括這4條線:

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

這些線路不應該存在

所以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"> 

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


    </FrameLayout> 

    <TabWidget android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom"/> 

    </LinearLayout> 
    </TabHost> 
相關問題