如何在Android中的选项卡中创建选项卡?

时间:2011-01-08 12:19:28

标签: android tabs

当我点按标签时,如何让其他三个标签可供点按?

我是Android新手,我正在做一个简单的项目。

3 个答案:

答案 0 :(得分: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;
    }
}

希望它有所帮助。

答案 1 :(得分:0)

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

答案 2 :(得分: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