2011-12-30 51 views
1

我需要創建一個單獨的活動tabhost,理想情況下創建選項卡和視圖,並動態地使用小部件填充視圖。在屏幕底部有一個「控制」按鈕很重要。如何使用並動態填充多個tabhost視圖

我能夠使用三種佈局創建三個選項卡。獲取EditTexts到a)出現,b)排列在[使用分隔符]選項卡下面是一個挑戰,由現有的StackOverflow答案協助。

我現在無法解決的是爲什麼第二個佈局/選項卡上的EditTexts不出現,而第一個選項卡上的EditTexts是這樣做的?

佈局

<?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="wrap_content" 
    android:layout_height="wrap_content"> 
    <RelativeLayout 
     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"/> 
      <View 
       android:id="@+id/separator" 
       android:layout_below="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="2dip" /> 
      <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_below="@+id/separator" 
      android:layout_above="@+id/btnSend" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <LinearLayout 
       android:id="@+id/tabview1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 
       <TextView 
       android:id="@+id/ll1text" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:text="ll1text" /> 
      </LinearLayout> 
      <LinearLayout 
       android:id="@+id/tabview2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 
      <TextView 
       android:id="@+id/ll2text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:text="ll2text" /> 
      </LinearLayout> 
      <RelativeLayout 
       android:id="@+id/tabview3" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" > 
      <TextView 
       android:id="@+id/rl3text" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:layout_alignParentBottom="true" 
       android:text="ll3text" /> 
      </RelativeLayout> 
      </FrameLayout> 
     <Button 
      android:layout_alignParentBottom="true" 
      android:text="Start Travelling" 
      android:id="@+id/btnSend" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" /> 
    </RelativeLayout> 
</TabHost> 

活動代碼

setContentView(R.layout.template); 
    TabHost th = getTabHost(); 

th.addTab(th.newTabSpec("tab1").setIndicator("Customer").setContent(R.id.tabview1)); 
    LinearLayout ll = (LinearLayout) findViewById(R.id.tabview1); 
    EditText et1 = new EditText (this); 
    et1.setText("ET1"); 
    ll.addView(et1); 
    TextView tv2 = new TextView(this); 
    tv2.setText("et2:"); 
    ll.addView(tv2); 
    EditText et2 = new EditText (this); 
    et2.setText("ET2"); 
    ll.addView(et2); 

    th.addTab(th.newTabSpec("tab2").setIndicator("Job").setContent(R.id.tabview2)); 
    ll = (LinearLayout) findViewById(R.id.tabview2); 
    EditText et21 = new EditText (this); 
    et21.setText("et21"); 
    ll.addView(et21); 
    EditText et22 = new EditText (this); 
    et22.setText("et22"); 
    ll.addView(et22); 
    EditText et23 = new EditText (this); 
    et23.setText("et23"); 
    ll.addView(et23); 

    th.addTab(th.newTabSpec("tab3").setIndicator("Tab 3").setContent(R.id.tabview3)); 
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.tabview3); 
    EditText et3 = new EditText (this); 
    et3.setText("ET3"); 
    rl.addView(et3); 

    th.setCurrentTab(0); 

怎麼會在第二個選項卡上的三個EditTexts不顯示?

回答

0

我認爲問題在於您試圖將所有選項卡放在一個活動下。

  • 使用的一類,與TabActivity
  • 涉及的每個標籤頁創建不同的活動
  • 添加tabhost佈局

退房this tutorial的幫助:應該按如下步驟將它們分開。