2015-11-02 148 views
1

我按照網上的教程中找到,但似乎不工作...Tabhost沒有顯示標籤

我得到的選項卡1總是打開的,這是好的,但我沒有看到選項卡菜單了。 ..

這裏是我的代碼:

Main2Activity:

public class Main2Activity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 

     // create the TabHost that will contain the Tabs 
     TabHost tabs=(TabHost)findViewById(R.id.tabhost); 

     tabs.setup(); 

     TabHost.TabSpec spec=tabs.newTabSpec("tag1"); 

     spec.setContent(R.id.tab1); 
     spec.setIndicator("Analog Clock"); 
     tabs.addTab(spec); 

     spec=tabs.newTabSpec("tag2"); 
     spec.setContent(R.id.tab2); 
     spec.setIndicator("DigitalClock"); 
     tabs.addTab(spec); 

     spec=tabs.newTabSpec("tag3"); 
     spec.setContent(R.id.tab3); 
     spec.setIndicator("Button"); 
     tabs.addTab(spec); 
    } 

Content_main2.xml:

<?xml version="1.0" encoding="utf-8"?> 
<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"> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <AnalogClock android:id="@+id/tab1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"/> 
      <DigitalClock android:id="@+id/tab2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"/> 
      <Button android:id="@+id/tab3" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:text="Do Nothing"/> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

This is how it looks when i run it

+0

http://stackoverflow.com/questions/11918488/android-how-do-you-set-up-the-tabhost –

回答

1

我解決了它:

setContentView(R.layout.content_main2); 

我打電話activity_main2,但不知道爲什麼,當我打電話activity_main2沒有得到content_main2。

我在Activity_main2.xml:

<include layout="@layout/content_main2" /> 

有誰知道爲什麼嗎?

0

你可以使用seperate activityeach tab。正如「intelliJ」所說的那樣。爲每個tab創建seperate activity和文件,然後分別在每個活動中添加模擬數字和按鈕。說在activity1analogactivity2digital等等。