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

发布于 2024-10-11 00:26:18 字数 67 浏览 2 评论 0原文

当我点击一个选项卡时,如何让另外三个选项卡可供点击?

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

When I tap a tab, how do I make there to be three more tabs available to tap?

I'm new to Android, and I'm doing a simple project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸印温柔 2024-10-18 00:26:18

您可以按照下面的示例代码进行操作(动态创建控件)。如果您愿意,还可以按照以下逻辑与 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;
    }
}

希望有帮助。

You can follow my sample code below (create controls on the fly). If you want to, you can also follow the logic below for use with xml layout.

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;
    }
}

Hope it helps.

浮光之海 2024-10-18 00:26:18

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

If you are using a TabActivity with your tab layout defined in XML, then simply make another TabActivity and other layout file, and in the first one, when you are making the intents, one of the intents would be the 2nd TabActivity.

长伴 2024-10-18 00:26:18

您最好制作一个

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

You'd be better off making a

extends TabActivity

rather than

extends Activity

So for example, here is the code:

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);

}

The xml can be found in the below link also. Good resources on this type of design can be found here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文