如何动态更改 Android 选项卡文本?
这看起来应该很简单,但我想不出办法。我需要一个选项卡来包含开始文本,但在用户从列表中选择一个项目后该文本会发生更改。我知道如何更改选项卡背景和颜色
mTabHost.getChildAt(index).setBackgroundColor();
,但没有更改选项卡指示器的选项。我尝试过使用 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");
}
也不起作用,还有其他一些尝试。有什么想法吗?提前致谢!
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();
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试这个对我有用的最简单的方法:
try this easiest way which works for me :
哇。好吧,这很痛苦。显然,TabWidget 使用relativelayout 做了一些奇怪的事情,每次我尝试使用radek-k 的解决方案做任何事情时,它都会因relativelayout 错误而崩溃。所以基本上解决方法如下。它还允许您更改字体、字体大小、字体颜色等
或在一行中...
其中“textIndex”是文本字段的索引。在本例中为 1。如果选项卡有图标或自定义修改,则索引可能会更改。
再次感谢 radek-k。你绝对给我指明了正确的方向。
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.
or in one line...
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.
Thanks again to radek-k. You definitely got me pointed in the right direction.
您可以使用
findViewById
在不知道TextView
的魔法索引的情况下完成此操作:You can do it without knowing the magic index of the
TextView
by usingfindViewById
:这是您的布局:
这是您的代码:
Here is your layout:
Here is your code:
你可以这样做
,其中
android.R.id.title
是系统生成的,只需根据您的需要更改 ChildIndex 和 TextYou can do it like this
Where
android.R.id.title
is System generated, just change ChildIndex and Text according to your need这很简单。尝试一下。有用。
This is simple and easy. Try it. It works.