如何动态更改android标签文本? [英] How to change android tab text on the fly?

查看:20
本文介绍了如何动态更改android标签文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来应该很简单,但我想不出办法.我需要一个选项卡来显示开始文本,但是在用户从列表中选择一个项目后更改该文本.我知道如何通过

This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via

mTabHost.getChildAt(index).setBackgroundColor();

但没有更改选项卡指示器的选项.我试过使用 EditText.

but there isn't an option to change the tab's indicator. I've tried using an EditText.

private EditText tabName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistics);

    comm = new Communicator();

    tabName = new EditText(this);
    tabName.setText("BeginningText");

    mTabHost = getTabHost();
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_1_stat")
                   .setIndicator(User)
                   .setContent(R.id.meStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_2_stat")
                   .setIndicator(tabName.getText())
                   .setContent(R.id.themStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_3_stat")
                   .setIndicator("Archive")
                   .setContent(R.id.archiveStatsTab));
    mTabHost.setOnTabChangedListener(this);
    getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
    mTabHost.setCurrentTab(0);

    onTabChanged("tab_1_stat");

}

.....

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    tabName.setText("ChangedTabText");
    Bundle extras = intent.getExtras();
    themStats = extras.getStringArray("themStats");
    mTabHost.setCurrentTab(1);
    onTabChanged("tab_2_stat");
}

这也不起作用,以及其他一些尝试.有任何想法吗?提前谢谢!

That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!

推荐答案

哇.好吧,这很痛苦.显然 TabWidget 用 RelativeLayout 做了一些时髦的事情,每次我尝试用 radek-k 的解决方案做任何事情时,它都会因 RelativeLayout 错误而爆炸.所以基本上解决方法如下.它还允许您更改字体、字体大小、字体颜色等.

Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.

TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");

或者在一行中...

((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");

其中textIndex"是文本字段的索引.在本例中为 1.如果选项卡有图标或自定义修改,则索引可能会更改.

where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.

再次感谢 radek-k.你肯定让我指出了正确的方向.

Thanks again to radek-k. You definitely got me pointed in the right direction.

这篇关于如何动态更改android标签文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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