2010-08-05 34 views
0

我是這個平臺的新手,已經開始喜歡它了。我正在爲使用Tabhost使用TabActivity類顯示兩個單獨的 活動的項目在UI上工作 。這工作正常。 現在我想添加一個viewflipper的方程。我正在嘗試 使用addView()方法將tabhost小部件添加到viewflipper。 如:在viewflipper中添加一個tabhost

public class Main extends TabActivity { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       TabHost host = getTabHost(); 
       host.addTab(host.newTabSpec("one").setIndicator("First").setContent( 
           new Intent(this, First.class))); 
       host.addTab(host.newTabSpec("two").setIndicator("Second") 
           .setContent(new Intent(this, Second.class))); 
       Button btn1 = new Button(this); 
       btn1.setText("Second Screen"); 
       flipper = new ViewFlipper(this); 
       flipper.addView(host); 
       flipper.addView(btn1); 
       setContentView(flipper); 
     } 
} 

的主要動機是有兩個活動的所有標籤式的應用程序。 viewflipper然後將在這兩個活動之間翻轉。我是 認爲另一種方法是使用xml佈局來保存tabhost ,並將其包含在視圖中。 我現在正在避免它,因爲那意味着我會再次爲這兩個類編寫代碼 。 要整理所有這些,有沒有辦法在瀏覽器中包含tabhost。

感謝,

新的人

回答

1

你應該把它寫在XML內,而不是硬編碼。它更容易,看起來更好。

在這裏,我已經創建了一個tabhost,其持有的2名列表視圖一個ViewFlipper,並在其他選項卡的列表視圖的另一個

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+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"> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff" 
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:text="@string/title_text" 
    android:id="@+id/title" 
    android:background="@drawable/shape" 
    android:padding="15dp" 
    android:gravity="center" 
    /> 

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

     <ListView android:id="@+id/listfav" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_weight="1"> 
     </ListView> 

     <ViewFlipper android:id="@+id/viewflipper" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
     > 
     <ListView android:id="@+id/listmain" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_weight="1"> 
     </ListView> 
     <ListView android:id="@+id/listsub" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_weight="1"> 
     </ListView> 
     </ViewFlipper> 

    </FrameLayout> 

    <TabWidget android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
    /> 

</LinearLayout> 

這是你怎麼做..

相關問題