2011-01-08 117 views

回答

0

如果您将TabActivity与您在XML中定义的选项卡布局一起使用,那么只需创建另一个TabActivity和其他布局文件,并且在第一个布置文件中,当您制作意图时,其中一个意图是第二个TabActivity 。

3

您可以按照下面的示例代码(即时创建控件)。如果你愿意,你也可以按照下面的逻辑来使用xml布局。

package com.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.TypedValue; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
import android.widget.TabHost; 
import android.widget.TabWidget; 
import android.widget.TextView; 
import android.widget.TabHost.TabSpec; 

public class DynamicTabDemo extends Activity { 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     View view = createTab(); 
     setContentView(view); 
    } 

    private android.view.ViewGroup createTab() { 
     TabHost tabHost = new TabHost(this); 
     tabHost.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     TabWidget tabWidget = new TabWidget(this); 
     tabWidget.setId(android.R.id.tabs); 
     tabHost.addView(tabWidget, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     FrameLayout frameLayout = new FrameLayout(this); 
     frameLayout.setId(android.R.id.tabcontent); 
     frameLayout.setPadding(0, 65, 0, 0); 
     tabHost.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     tabHost.setup(); 

     TabSpec tabParent = tabHost.newTabSpec("Parent"); 
     tabParent.setIndicator("Tab Parent"); 
     tabParent.setContent(new TabHost.TabContentFactory() { 
      public View createTabContent(String tag) { 
       LinearLayout panel = new LinearLayout(DynamicTabDemo.this); 
       panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

       TabHost tabHost = new TabHost(getApplicationContext()); 
       tabHost.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

       TabWidget tabWidget = new TabWidget(getApplicationContext()); 
       tabWidget.setId(android.R.id.tabs); 
       tabHost.addView(tabWidget, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

       FrameLayout frameLayout = new FrameLayout(getApplicationContext()); 
       frameLayout.setId(android.R.id.tabcontent); 
       frameLayout.setPadding(0, 65, 0, 0); 
       tabHost.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

       tabHost.setup(); 

       TabSpec tabChild1 = tabHost.newTabSpec("Child_1"); 
       tabChild1.setIndicator("Child 1"); 
       tabChild1.setContent(new TabHost.TabContentFactory() { 
        public View createTabContent(String tag) { 
         TextView txt = new TextView(DynamicTabDemo.this); 
         txt.setText("Test Child 1"); 
         txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f); 
         txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
         return txt; 
        } 
       }); 
       tabHost.addTab(tabChild1); 

       TabSpec tabChild2 = tabHost.newTabSpec("Child_2"); 
       tabChild2.setIndicator("Child 2"); 
       tabChild2.setContent(new TabHost.TabContentFactory() { 
        public View createTabContent(String tag) { 
         TextView txt = new TextView(DynamicTabDemo.this); 
         txt.setText("Test Child 2"); 
         txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f); 
         txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
         return txt; 
        } 
       }); 
       tabHost.addTab(tabChild2); 

       TabSpec tabChild3 = tabHost.newTabSpec("Child_3"); 
       tabChild3.setIndicator("Child 3"); 
       tabChild3.setContent(new TabHost.TabContentFactory() { 
        public View createTabContent(String tag) { 
         TextView txt = new TextView(DynamicTabDemo.this); 
         txt.setText("Test Child 3"); 
         txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f); 
         txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
         return txt; 
        } 
       }); 
       tabHost.addTab(tabChild3); 

       panel.addView(tabHost); 
       return panel; 
      } 
     }); 
     tabHost.addTab(tabParent); 

     return tabHost; 
    } 
} 

希望它有帮助。

+0

这是给我的资源不可在tabHost,addTab(tabParent)发现异常; – 2013-10-06 06:45:07

0

你会更好作出

extends TabActivity 

而不是

extends Activity 

因此,举例来说,这里是代码:

public class HelloTabWidget extends TabActivity { 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

Resources res = getResources(); // Resource object to get Drawables 
TabHost tabHost = getTabHost(); // The activity TabHost 
TabHost.TabSpec spec; // Resusable TabSpec for each tab 
Intent intent; // Reusable Intent for each tab 

// Create an Intent to launch an Activity for the tab (to be reused) 
intent = new Intent().setClass(this, ArtistsActivity.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
        res.getDrawable(R.drawable.ic_tab_artists)) 
       .setContent(intent); 
tabHost.addTab(spec); 

// Do the same for the other tabs 
intent = new Intent().setClass(this, AlbumsActivity.class); 
spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
        res.getDrawable(R.drawable.ic_tab_albums)) 
       .setContent(intent); 
tabHost.addTab(spec); 

intent = new Intent().setClass(this, SongsActivity.class); 
spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
        res.getDrawable(R.drawable.ic_tab_songs)) 
       .setContent(intent); 
tabHost.addTab(spec); 

tabHost.setCurrentTab(2); 

}

的xml也可以在下面的链接中找到。在这种类型的设计很好的资源,可以在这里找到:

http://www.google.co.uk/search?q=create+tabs+in+android&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a