2013-02-01 35 views
2

嘿,我對創建android應用程序相當陌生。我想在操作欄中創建三個選項卡。我已經通過使用New-> Android Activity->使用Swipe和Tabs導航來創建它們。Android中的選項卡,如何創建內容

這就是我在MainActivity的代碼(使用android.com的教程之一):

public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      // Create a new TextView and set its text to the fragment's section 
      // number argument value. 
      int current_section = getArguments().getInt(ARG_SECTION_NUMBER); 

      switch(current_section){ 
      case 1: 
      { 

       EditText element1 = new EditText(getActivity()); 
       element1.setHint(R.string.edit_text_hint); 
       element1.setGravity(Gravity.TOP); 
       element1.setPadding(40,40,40,0); 
       element1.setSingleLine(); 

       TextView textView = new TextView(getActivity()); 
       textView.setGravity(Gravity.CENTER); 
       textView.setText("Search"); 


       return element1; 
      } 
      case 2: 
      { 
       TextView textView = new TextView(getActivity()); 
       textView.setGravity(Gravity.CENTER); 
       textView.setText("User Account"); 
       return textView; 
      } 
      case 3: 
      { 
       TextView textView = new TextView(getActivity()); 
       textView.setGravity(Gravity.CENTER); 
       textView.setText("Last Updated"); 
       return textView; 
      } 
      } 
      TextView textView1 = new TextView(getActivity()); 
      textView1.setGravity(Gravity.CENTER); 
//   textView.setText(Integer.toString(getArguments().getInt(
//     ARG_SECTION_NUMBER))); 
      return textView1; 
     } 
    } 
在三個選項卡的

所以就有了不同的項目顯示,很遺憾,只能顯示一個 - 而不是更多。我認爲這是一個相當簡單的問題,但我只是沒有弄清楚這是如何工作的。 希望你能幫助我。

回答

2

在本教程中,您將返回一個textview。嘗試將文本視圖添加到佈局,然後返回佈局。

我想你想要的是使用不同的標籤片段。然後,您可以在片段本身中指定佈局和其他代碼,而不是使用開關盒構建一個BIG片段。

對於每個片段你創建一個新的類。這片段可以被添加到一個TabBarListener

onCreateView的片段,您可以通過將對象添加到視圖參數編程創建您的佈局。您也可以使用充氣器充氣您的XML佈局。

我創建了一個示例項目,分別爲1 片段視圖膨脹,另一種是編程方式創建:

here

如果你讀了Android文檔也將更加清晰。我認爲他們甚至有一個示例代碼項目。

Actiobar, the tab sections

More info about fragments

+0

好吧,我想類似的東西。那麼究竟該怎麼做呢?我應該爲每個選項卡使用單獨的xml文件還是爲MainActivity.class中的每個選項卡定義佈局?感謝您的快速幫助:) – chrissik

+0

我更新了答案。花一些時間閱讀文檔。它在那裏。 – Oritm

+0

並且不要忘記在答案左側標記(V)/ Upvote(^)正確的答案;) – Oritm

相關問題