Android 自定义选项卡链接到活动

发布于 2024-10-09 02:51:56 字数 1668 浏览 2 评论 0原文

我如何让下面的类创建链接到活动的选项卡。它按原样工作,但选项卡未链接到任何活动。我需要选项卡来加载不同的活动吗?

import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;

public class CustomTabActivity extends TabActivity {
    TabHost mTabHost = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        // mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
        setupTab(new TextView(this), "Tab 1");
        setupTab(new TextView(this), "Tab 2");
        setupTab(new TextView(this), "Tab 3");
    }

    private void setupTab(final View view, final String tag) {
        View tabview = createTabView(mTabHost.getContext(), tag);
        TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
                .setContent(new TabContentFactory() {

                    public View createTabContent(String tag) {
                        return view;
                    }
                });
        mTabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
    }
}

How can i get the class below to create tabs that link to activities. its worrking the way it is but the tabs are not linked to any activity. I need the tabs to load different activities?

import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;

public class CustomTabActivity extends TabActivity {
    TabHost mTabHost = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        // mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
        setupTab(new TextView(this), "Tab 1");
        setupTab(new TextView(this), "Tab 2");
        setupTab(new TextView(this), "Tab 3");
    }

    private void setupTab(final View view, final String tag) {
        View tabview = createTabView(mTabHost.getContext(), tag);
        TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
                .setContent(new TabContentFactory() {

                    public View createTabContent(String tag) {
                        return view;
                    }
                });
        mTabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
    }
}

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

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

发布评论

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

评论(1

往昔成烟 2024-10-16 02:51:56

我需要将Conent 设置为“活动”而不是“视图”

不,您不需要。让您的选项卡充满活动不会给您带来任何好处。事实上,这样做弊大于利——浪费了堆空间、堆栈空间和 CPU 时间,却没有任何附加值。

现在,如果您不愿意,则不必在运行时定义选项卡内容。例如,这是一个定义选项卡内容的示例项目TabHost 本身的布局相同。

I need to setConent to Activities and not Views

No, you do not. You gain nothing from having your tabs populated with activities. In fact, doing that causes more harm than good -- you waste heap space, stack space, and CPU time for no added value.

Now, you do not have to define your tab content at runtime if you do not want. For example, here is a sample project that defines the tab content in the same layout as the TabHost itself.

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