2013-01-10 73 views
0

我正在試驗TabHost小部件。我沒有意識到TabActivity已被棄用。當我運行我的活動時,實際上在屏幕上顯示我的TabHost時遇到麻煩。目前我的XML文件包含以下內容:如何在不擴展TabActivity的情況下顯示選項卡視圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 

    <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/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </LinearLayout> 

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

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

而且我MainActivity.java包含:

public class MainActivity extends Activity { 

TabHost myTabBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

我怎樣才能獲得TabHost出現在屏幕上?每次我谷歌TabHost所有的教程涉及擴展TabActivity!

謝謝。

編輯:我在正確的軌道上做這樣的事情嗎?在標籤

myTabBar = (TabHost)findViewById(android.R.id.tabhost); 
    myTabBar.setup(); 

回答

0

活動已被廢棄和谷歌同意,這是一個壞主意,要做到這一點,你必須考慮使用的意見,你的標籤,here從CommonsWare書的爲例,你可以找到有用的資源的內容。

相關問題