Android TabHost - 每个选项卡中的 Activity

标签 android android-activity android-tabhost

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

有人知道如何实现这一点吗?

提前致谢

最佳答案

这是我的 Activity 示例,它也没有从 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 - 每个选项卡中的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3103062/

相关文章:

java - Eclipse - Android 设备选择器中的目标 "unknown"

Python 调用 MQ 代理和 DBus

java - 当用户单击主页按钮时终止 Activity

android - android v2 mapview 中的中心固定标记

android - 在android中动态添加标签

java - Tabhost 中的 Android FragmentActivity?

java - Android Eclipse - 看不到注释处理选项

android - 迁移以将新数据行插入 Room DB

android - 动态壁纸和扩展背景

android - Fragment 的 oncreateView() 中的 NullPointerException