0

As you can read in the title, i'd like to change dynamically the label in my tabs. The thing is, im using a ta host and tabspec to create my tabs, here an example of a tab :

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

When i come and say sourceSelect.setText("") it wont do anything, it compiles tho.

Any ideas ?

Thanks!

4

1 回答 1

0

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

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

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

当然,您可以将两者结合起来——如果您使用自定义选项卡指示器并且其文本视图的 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.");

希望这可以帮助。

于 2013-05-30T12:35:55.160 回答