2014-01-08 55 views
0

我是新來的android開發,我有以下xml佈局和代碼來添加選項卡內的TabActivity,但選擇適當的選項卡不顯示。我已經多次改變佈局,但無法使標籤出現。TabHost只顯示一個內容

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="600dp" 
    android:layout_height="880dp" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="600dp" 
     android:layout_height="840dp" 
     android:layout_weight="1" > 

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

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 
      </FrameLayout> 
     </TabWidget> 
    </LinearLayout> 

    <GridLayout 
     android:layout_width="600dp" 
     android:layout_height="40dp" 
     android:layout_weight="1" 
     android:columnCount="4" 
     android:rowCount="1" > 

     <ImageButton 
      android:id="@+id/button_scan" 
      style="android:attr/buttonBarButtonStyle" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_row="0" 
      android:onClick="doScan" 
      android:src="@drawable/lupa" /> 

     <ImageButton 
      android:id="@+id/button_foto" 
      style="android:attr/buttonBarButtonStyle" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_row="0" 
      android:src="@drawable/camera" /> 

     <ImageButton 
      android:id="@+id/button_pinteresses" 
      style="android:attr/buttonBarButtonStyle" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_column="2" 
      android:layout_row="0" 
      android:src="@drawable/globe" /> 

     <ImageButton 
      android:id="@+id/button_help" 
      style="android:attr/buttonBarButtonStyle" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_column="3" 
      android:layout_row="0" 
      android:src="@drawable/question" /> 
    </GridLayout> 
</LinearLayout> 

</TabHost> 

而在TabActivity:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TabHost tabHost; 
    tabHost = getTabHost(); 
    tabHost.setVisibility(TabHost.VISIBLE); 

    TabSpec tab1 = tabHost.newTabSpec("TabIndoor"); 
    TabSpec tab2 = tabHost.newTabSpec("TabOutdoor"); 

    tab1.setIndicator("Indoor"); 
    tab1.setContent(new Intent(this, com.paar.ch9.MainActivity.class)); 

    tab2.setIndicator("Outdoor"); 
    tab2.setContent(new Intent(this, com.paar.ch9.MainActivity.class)); 

    tabHost.addTab(tab1); 
    tabHost.addTab(tab2); 
} 

我怎樣才能讓標籤可見?

回答

0

您需要使用則tabspec以及 - 我沒有時間來檢驗,但在你的Java代碼,試試這個並實現onTabChangeListener:

public class Main extends TabActivity implements OnTabChangeListener { 

TabHost tabHost; 

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

    setContentView(R.layout.activity_main); 

    tabHost = getTabHost(); 
    tabHost.getTabWidget().setStripEnabled(false); 
    tabHost.setOnTabChangedListener((OnTabChangeListener) this); 

    TabSpec spec1 = tabHost.newTabSpec("Indoor"); 
    //below sets the text and image on the tab 
    spec1.setIndicator("", getResources().getDrawable(R.drawable.indoorImage)); 
    Intent intIndoor = new Intent(this, Indoor.class); 
    spec1.setContent(intIndoor); 

    TabSpec spec2 = tabHost.newTabSpec("Outdoor"); 
    //below sets the text and image on the tab 
    spec2.setIndicator("Outdoor", getResources().getDrawable(R.drawable.outdoorImage)); 
    Intent intOutdoor = new Intent(this, Outdoor.class); 
    spec2.setContent(intOutdoor); 

    tabHost.addTab(spec1); 
    tabHost.addTab(spec2); 
} 

而且你的XML代碼可以簡單這樣的:

<?xml version="1.0" encoding="utf-8"?> 
    <TabHost 
    android:id="@android:id/tabhost" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" > 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" > 
    </TabWidget> 

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

    </TabHost> 
+0

Thamk你中芯國際,我想你指示的變化,即使我需要在網格佈局的按鈕,但結果卻是一樣的,只是一個com.paar.ch9.MainActivity顯示出來,並沒有標籤。 – ncarneiro

+0

好吧,您是否嘗試過放置多個活動,例如創建活動one.class和two.class(而不是重複的com.paar.ch9.MainActivity.class,並且只使用MainActivity來擴展實現OnTabChangeListener的TabActivity) - 可能會有所幫助,而且我認爲,如果代碼要工作,您可能需要將其全部替換爲我的代碼,然後您可以根據您的規範對其進行自定義,以實現佈局功能。 – Tana

+0

謝謝你,我照你的意思做了,得到你的佈局,並從中再次建立我的。現在工作很好,再次感謝。 – ncarneiro