创建后更改“蜂窝操作栏”选项卡中的选项卡文本
我试图通过创建一个使用操作栏和选项卡的简单文本编辑应用程序来使用 Android Honeycomb。不过我遇到了一个恼人的问题。创建选项卡并将其添加到操作栏后,我想更改选项卡上显示的文本。我认为使用以下方法 ActionBar.Tab.setText(CharSequence arg0) 可以解决问题,但是,它似乎并没有改变可视文本。更奇怪的是,如果我调用 getText(),它会返回我将选项卡更改为的文本。下面是我用来更改选项卡文本的代码片段:
int currentTabIndex = ab.getSelectedNavigationIndex();
currentTabTitle = (String) ab.getTabAt(currentTabIndex).getText(); // just to check
ab.getTabAt(currentTabIndex).setText(fileName); // change tab text
currentTabTitle = (String) ab.getTabAt(currentTabIndex).getText(); // just to check
我真的很茫然,到处都在搜索。我将非常感谢任何人提出的任何建议。感谢您抽出时间。
I'm trying to get use to Android Honeycomb by creating a simple text editing application which utilizes the Action Bar and tabs. I am running into an annoying issue though. After a tab has been created and added to the Action Bar I would like to change the text displayed on the tab. I thought that using the following method, ActionBar.Tab.setText(CharSequence arg0) would do the trick, however, it doesn't seem to be changing the viewable text. What's weirder still is that if I were to call getText() it returns the text that I changed the tab to. Below is a snippet of code that I am using to change the tab text:
int currentTabIndex = ab.getSelectedNavigationIndex();
currentTabTitle = (String) ab.getTabAt(currentTabIndex).getText(); // just to check
ab.getTabAt(currentTabIndex).setText(fileName); // change tab text
currentTabTitle = (String) ab.getTabAt(currentTabIndex).getText(); // just to check
I really am at a loss and have searched everywhere. I would greatly appreciate any advice that anyone has. Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个愚蠢的问题,添加和删除选项卡是一个坏主意,因为如果您使用片段,您最终将删除并重新添加带有选项卡的片段。使用自定义视图似乎效果更好,并且额外的好处是可以为您提供更好的自定义功能。
以下是如何制作具有与默认视图相同的自定义视图的选项卡:
这是像素完美的虚拟视图:
从这里我们可以更改选项卡上的图标和文本没有任何问题。示例:
...一切正常
This is kind of a silly issue and adding and removing tabs is a bad idea because if you're using fragments you will end up removing and re-adding your fragment with its tab. Using a custom view seems to work much better and as an added bonus offers you greater customization.
Here's how to make a tab with a custom view that looks and behaves identical to the default ones:
and here is the pixel perfect dummy view:
From here we can change icons and text on the tab without any problems at all. Example:
... and everything works as it should
尝试删除选项卡并在更改文本后将其重新添加到所需的索引处。 (这是一个错误。添加后设置文本时,关联的视图不会更新。)
Try removing the tab and re-adding it at the desired index after changing the text. (It's a bug. The associated view doesn't update when you set the text after adding.)