更改标签标签Android

时间:2013-05-30 12:22:38

标签: android tabs label android-tabhost

正如您可以在标题中看到的那样,我想动态更改标签 在我的标签中。 问题是,我使用ta主机和tabspec来创建我的标签,这里是一个标签的例子:

    TabSpec sourceSelect = tabHost.newTabSpec("Source Select");
    // setting Title and Icon for the Tab
    sourceSelect.setIndicator("Source Select");
    final Intent selectIntent = new Intent(this, FileListActivity.class);
    sourceSelect.setContent(selectIntent);

当我来说 sourceSelect.setText(“”)它不会做任何事情时,它会编译。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您没有为选项卡指示器使用自定义布局,则必须通过选项卡窗口小部件中的索引获取选项卡规范(基于0)。这是一个相当蹩脚的实现,但我搜索并搜索了我的应用程序的答案,这是迄今为止唯一有效的方法:

((TextView) tabHost.getTabWidget().getChildAt(0)
            .findViewById(android.R.id.title)).setText("This sucks, I know.");

另一个选择是使用setIndicator(视图视图)并输入带有所需文本的文本视图,如果您不害怕垃圾收集器。

当然,您可以将两者结合起来 - 如果您使用的是自定义标签指示器,其文本视图的ID就是,例如,R.id.text_view_tab:

((TextView) tabHost.getTabWidget().getChildAt(0)
            .findViewById(R.id.text_view_tab)).setText("This is still ugly and hacky, though.");

希望这有帮助。

相关问题