2011-07-13 50 views
0

我在我的應用程序中使用android選項卡。我有一個主要的課程,我在那裏輸入我的標籤。有4個選項卡,所以我有每個1個Java類。但在每個標籤中,我也會去其他意圖。當我從一個選項卡移動到其他意圖時,選項卡視圖消失。我希望我的標籤保留在所有課程和所有視圖中。我應該爲此做些什麼。所以這是我主代碼類的代碼。Android TabHost問題

意圖intent1; 意圖intent2; 意圖intent3; 意圖intent4;

intent1 = new Intent().setClass(this, x.class); 

    spec = tabHost 
      .newTabSpec("x") 
      .setIndicator("x", 


    tabHost.addTab(spec); 

    intent2 = new Intent().setClass(this, y.class); 
    spec = tabHost 
      .newTabSpec("y") 
      .setIndicator("y", 


    tabHost.addTab(spec); 

    intent3 = new Intent().setClass(this,z.class); 
    spec = tabHost 
      .newTabSpec("z") 
      .setIndicator("z", 


    tabHost.addTab(spec); 

    intent4 = new Intent().setClass(this, a.class); 
    spec = tabHost 
      .newTabSpec("a") 
      .setIndicator("a", 


    tabHost.addTab(spec); 

回答

2

希望這段代碼可以幫助你....

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:admobsdk="http://schemas.android.com/apk/res/com.admob.android.example" 
    android:orientation="vertical"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 



<LinearLayout 
    android:id="@+id/llTab" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:visibility="visible" 
    android:layout_weight="2"> 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:focusable="false" 
     android:focusableInTouchMode="false"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"     
      android:background="@android:color/darker_gray"    
      android:nextFocusUp="@android:id/tabcontent" 
      android:layout_gravity="bottom" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:paddingBottom="65dp" 
      android:focusable="false" 
      android:focusableInTouchMode="false"/> 

    </TabHost> 

</LinearLayout> 

活動:

public class ActivityGroup extends TabActivity { 
/** Called when the activity is first created. */ 

    private static final String TAG_TYERS = "Tab1"; 
    private static final String TAG_BABES = "Tab2"; 
    private static final String TAG_TEAMS = "Tab3"; 
    private static final String TAG_EVENTS = "Tab4"; 
    TabHost tabHost; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tabHost = getTabHost(); 

    tabHost.addTab(tabHost.newTabSpec("Tab1") 
     .setIndicator(TAG_TYERS) 
     .setContent(new Intent().setClass(this, Tab1.class))); 

    tabHost.addTab(tabHost.newTabSpec("Tab2") 
     .setIndicator(TAG_BABES) 
     .setContent(new Intent().setClass(this, Tab2.class))); 

    tabHost.addTab(tabHost.newTabSpec("Tab3") 
     .setIndicator(TAG_EVENTS) 
     .setContent(new Intent().setClass(this, Tab3.class))); 

    tabHost.addTab(tabHost.newTabSpec("Tab4") 
     .setIndicator(TAG_EVENTS) 
     .setContent(new Intent().setClass(this, Tab4.class))); 

    tabHost.setCurrentTab(0); 
}