Android TabHost - 每个选项卡中的活动 [英] Android TabHost - Activities within each tab

查看:27
本文介绍了Android TabHost - 每个选项卡中的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建多个标签,每个标签都有不同的活动.唯一的缺点是我使用的是自定义布局文件,因此我的类扩展了 Activity 而不是 TabActivity.尝试运行时,它失败并建议调用 TabHost.Setup(ActivityGroupManager agm)

I'm trying to create multiple Tabs, each with a different Activity. The only downside is i'm using a custom layout file thus my class extends an Activity rather than a TabActivity. While trying to run, it fails and suggests calling TabHost.Setup(ActivityGroupManager agm)

有人知道如何实现这一点的想法/实际例子吗?

Anyone have an idea/practical example of how this can be achieved?

提前致谢

推荐答案

这是我的 Activity 示例,它也没有从 TabActivity 扩展:

This is a sample of my activity that also doesn't extend from TabActivity:

protected TabHost tabs;

// ...

/**
 * Init tabs.
 */
private void initTabs() {
    tabs = (TabHost) findViewById(R.id.tabhost);
    tabs.setup();
    tabs.setBackgroundResource(R.drawable.bg_midgray);

    TabHost.TabSpec spec;

    // Location info
    txtTabInfo = new TextView(this);
    txtTabInfo.setText("INFO");
    txtTabInfo.setPadding(0, 0, 0, 0);
    txtTabInfo.setTextSize(14);
    txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive);
    txtTabInfo.setTextColor(Color.DKGRAY);
    txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    txtTabInfo.setHeight(39);
    spec = tabs.newTabSpec("tabInfo");
    spec.setContent(R.id.tabInfo);
    spec.setIndicator(txtTabInfo);
    tabs.addTab(spec);

    // Maps
    txtTabMap = new TextView(this);
    txtTabMap.setText("MAP");
    txtTabMap.setTextSize(14);
    txtTabMap.setPadding(0, 0, 0, 0);
    txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active);
    txtTabMap.setTextColor(Color.DKGRAY);
    txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    txtTabMap.setHeight(39);
    spec = tabs.newTabSpec("tabMap");
    spec.setContent(R.id.tabMap);
    spec.setIndicator(txtTabMap);
    tabs.addTab(spec);

    tabs.setCurrentTab(0);

    tabs.setOnTabChangedListener(this);
}

// ...

这篇关于Android TabHost - 每个选项卡中的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆