1

我想创建一个由两个选项卡组成的应用程序,每个选项卡都运行自己的 web 视图。我可以创建标签,但不能管理他们的 webviews。

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, fbActivity.class);

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

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


    tabHost.setCurrentTab(0);
}
4

1 回答 1

2

首先创建tabactivity类

public class tabviews extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);
    TabHost tab = getTabHost();

    TabSpec tab1 = (TabSpec) tab.newTabSpec("tb1");
    TabSpec tab2 = (TabSpec) tab.newTabSpec("tb2");


    tab1.setIndicator("Customer", null).setContent(
            new Intent(this, webview1.class));
    tab2.setIndicator("Item", null).setContent(
            new Intent(this, webview2.class));

    tab.addTab(tab1);
    tab.addTab(tab2);



}

}

然后创建两个活动类说 webview1.class 和 webview2.class

public class webview1 extends Activity
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
   WebView v = (WebView) findViewById(R.id.webView1);
   v.loadUrl("http://www.google.com"); 

然后用相同的代码创建另一个类。在 manifest.xml 中输入所有三个类

于 2013-03-19T10:39:32.420 回答