0

我已經創建了標籤作爲視圖。但是我沒有找到關於如何操作這些視圖的任何信息。例如,如果我想顯示一個列表視圖和一個窗體(tablelayout)在另一個。我可以爲每個標籤分別設計佈局嗎?更重要的是,我在哪裏包含關於每個選項卡的Java代碼?如何處理標籤作爲視圖?

這裏是我的Java代碼:

public class TabWorkEntryActivity extends TabActivity { 
/** Called when the activity is first created. */ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.tabworkentry); 

TabHost mTabHost = getTabHost(); 


    mTabHost.addTab(mTabHost.newTabSpec("top10").setIndicator("Top 10").setContent(R.id.Top_10)); 
    mTabHost.addTab(mTabHost.newTabSpec("billable").setIndicator("Billable").setContent(R.id.Billable)); 
    mTabHost.addTab(mTabHost.newTabSpec("product").setIndicator("Product").setContent(R.id.Product)); 
     mTabHost.addTab(mTabHost.newTabSpec("regular").setIndicator("Regular").setContent(R.id.General)); 

    mTabHost.setCurrentTab(3); 


} 

任何有點幫助表示讚賞。

回答

1

有把內容和處理標籤的方式主要有兩種:1。 您創建活動,並把活動的tabHost 2.你處理的內容,在您添加相同的代碼標籤(打擊所有安裝的東西) 我總是用第二個方法,因爲我的代碼是有組織的,所以它不是那麼大......

下面是一個簡單的佈局,與內容作爲另一個佈局...

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

    <TabHost 
     android:id="@+id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
      </TabWidget> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <LinearLayout 
        android:id="@+id/checkall_tab" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <include 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         layout="@layout/checkall" /> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/view_tab" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <include 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         layout="@layout/viewall" /> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/run_program" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <include 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         layout="@layout/programs" /> 
       </LinearLayout> 

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

</LinearLayout> 
+0

好的。現在,如果我使用第二種方法,我可以爲我的選項卡使用單獨的佈局,還是必須將所有內容都包含在單個佈局中,例如我的活動? – Harsh

+0

yourTabSpec.setContent(R.id.the_id_of_your_tab); –

+0

要放在tabView中的「佈局」必須在選項卡主機的佈局中指定...查看它是如何做到的 –

0

我用Android 4.0 tabView製作了這段代碼。嘗試瞭解它... 你得實例化TabHost,然後用這個對象創建標籤。 然後將該選項卡添加到TabHost。 要通過id查找視圖,您不需要在正在訪問的選項卡上傳遞「視圖」。

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

    tabHost = (TabHost) findViewById(R.id.tabhost); 
    tabHost.setup(); 

    checkAll = tabHost.newTabSpec("checkAll"); 
    viewAll = tabHost.newTabSpec("viewAll"); 
    runProgram = tabHost.newTabSpec("runProgram"); 

    viewAll.setContent(R.id.view_tab); 
    viewAll.setIndicator("View All"); 
    tabHost.addTab(viewAll); 

    checkAll.setContent(R.id.checkall_tab); 
    checkAll.setIndicator("Check All"); 
    tabHost.addTab(checkAll); 

    runProgram.setContent(R.id.run_program); 
    runProgram.setIndicator("Run program"); 
    tabHost.addTab(runProgram); 
} 
+0

我做了同樣的事情ING。我不是嗎?我的問題是現在假設我想'view_tab'作爲我的列表視圖然後我在哪裏寫我的java代碼相關的選項卡? – Harsh