2011-05-30 88 views
0

我想創建一個帶有選項卡的應用程序,並且我在Internet上找到了本指南http://developer.android.com/resources/tutorials/views/hello-tabwidget.html,我選擇了遵循。Android Tabs with TableLayout

我已經創建了一個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="50dip"> 
    <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"> 
     <TableLayout android:id="@+id/aTableLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     </TableLayout> 
    </FrameLayout> 
    </LinearLayout> 
</TabHost> 

我還創建了一個類來創建標籤由於指南,如下所示。

public class GuiTabs extends TabActivity { 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabslayout); 

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

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("Reg").setIndicator("Registration").setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTabByTag("Reg"); 
    } 
} 

現在我需要生成的標籤,其中一個選項卡是GuiRegistration的內容。
我設計了一個XML文件中的GuiRegistration,但我也需要添加動作到按鈕,例如,所以我必須使用GuiRegistration類。
但我怎麼能創建的標籤,因爲我一直在使用

setContentView(R.layout.registration) 

設計中的類

而且

public class GuiRegistration extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    View aTabLayout = findViewById(R.id.aTableLayout); 
    ViewGroup vGroup = (ViewGroup) aTabLayout.getParent(); 
    int index = vGroup.indexOfChild(aTabLayout); 
    vGroup.removeViewAt(index); 
    View newTabLayout = getLayoutInflater().inflate(R.layout.registration, vGroup, false); 
    vGroup.addView(newTabLayout, index); 
    } 
} 

但似乎沒有任何工作的標籤directy試過了,可有人告訴我如何使它工作?

+1

你的問題不清楚,請更具體地 – 2011-05-30 16:31:32

+0

我想你也需要tabhost。 – Tsunaze 2011-05-30 17:39:25

+0

我現在編輯了這個問題,所以我希望能夠更加明確 – The87Boy 2011-05-30 17:45:39

回答

0

從代碼,您Guiregistration類沒有調用 的setContentView(R.layout.registration)

可能這就是爲什麼認爲沒有在標籤繪製。

+0

這不是答案,但是當我找到另一個解決方案時,我接受你的答案是正確答案 – The87Boy 2012-01-04 21:45:13