创建后更改“蜂窝操作栏”选项卡中的选项卡文本

发布于 2024-11-03 15:51:20 字数 619 浏览 1 评论 0原文

我试图通过创建一个使用操作栏和选项卡的简单文本编辑应用程序来使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-11-10 15:51:20

这是一个愚蠢的问题,添加和删除选项卡是一个坏主意,因为如果您使用片段,您最终将删除并重新添加带有选项卡的片段。使用自定义视图似乎效果更好,并且额外的好处是可以为您提供更好的自定义功能。

以下是如何制作具有与默认视图相同的自定义视图的选项卡:

ActionBar bar = getActionBar();

TabListener tabListener = new TabListener() {

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
};

Tab tab1 = bar.newTab()
          .setText("Info")
          .setTabListener(tabListener)
          .setCustomView(makeTabDummy("Info", android.R.drawable.ic_menu_info_details));

bar.addTab(tab1);

这是像素完美的虚拟视图:

private TextView makeTabDummy(String text, int icon) {

    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setTextColor(0xffffffff);
    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setGravity(Gravity.CENTER);

    return tv;
}

从这里我们可以更改选项卡上的图标和文本没有任何问题。示例:

TextView tv = (TextView) tab1.getCustomView();          
tv.setText("change the text!");
tv.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.btn_star_big_on, 0, 0, 0);

...一切正常

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:

ActionBar bar = getActionBar();

TabListener tabListener = new TabListener() {

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
};

Tab tab1 = bar.newTab()
          .setText("Info")
          .setTabListener(tabListener)
          .setCustomView(makeTabDummy("Info", android.R.drawable.ic_menu_info_details));

bar.addTab(tab1);

and here is the pixel perfect dummy view:

private TextView makeTabDummy(String text, int icon) {

    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setTextColor(0xffffffff);
    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setGravity(Gravity.CENTER);

    return tv;
}

From here we can change icons and text on the tab without any problems at all. Example:

TextView tv = (TextView) tab1.getCustomView();          
tv.setText("change the text!");
tv.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.btn_star_big_on, 0, 0, 0);

... and everything works as it should

标点 2024-11-10 15:51:20

尝试删除选项卡并在更改文本后将其重新添加到所需的索引处。 (这是一个错误。添加后设置文本时,关联的视图不会更新。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文