2014-12-06 35 views
0

我有兩個活性和每一個具有在第二活動時,不同標籤,並按下一個選項卡意向第一活動選項卡在第二活動在第一活動標籤替換標籤在第一活動

java代碼替換標籤:

Resources ressources = getResources(); 
    TabHost tabHost = getTabHost(); 

    Intent intentAndroid = new Intent().setClass(this, calc.class); 
    TabHost.TabSpec tabSpecAndroid = tabHost 
      .newTabSpec("Android") 
      .setIndicator("", ressources.getDrawable(R.drawable.me)) 
      .setContent(intentAndroid); 


    Intent intentApple = new Intent().setClass(this, MainActivity.class); 
    TabHost.TabSpec tabSpecApple = tabHost 
      .newTabSpec("Apple") 
      .setIndicator("", ressources.getDrawable(R.drawable.myket)) 
      .setContent(intentApple); 


    tabHost.setCurrentTab(2); 

XML:

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

回答

0

你可以嘗試下面的代碼。它可以幫助你:

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

    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec; 
    Intent intent; 

    intent = new Intent().setClass(this, Tab1Activity.class); 
    spec = tabHost.newTabSpec("First").setIndicator("First") 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, Tab2Activity.class); 
    spec = tabHost.newTabSpec("Second").setIndicator("Second") 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, Tab3Activity.class); 
    spec = tabHost.newTabSpec("Third").setIndicator("Third") 
        .setContent(intent); 
    tabHost.addTab(spec); 

} 

和XML頁面:

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

     <LinearLayout 
       android:id="@+id/LinearLayout01" 
       android:orientation="vertical" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"> 

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

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

     </LinearLayout> 

</TabHost>