How to change the title of the Tab Dynamically

前端 未结 3 1991
花落未央
花落未央 2020-12-11 18:14

I have three tabs in my Application. On an event under one Tab, i want to change the title of an another existing Tab. This is the title that we provide while adding the tab

相关标签:
3条回答
  • 2020-12-11 18:39

    Unless you can find a cleaner method, you can access the TabWidget itself. Contained in the TabWidget are relative layouts for each of your tabs which each contain an ImageView and a TextView. To directly access the textview in the tab at index 0 you can do this:

    mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
    

    Then just cast as a TextView and you can edit it however you want. The below worked for me:

    ((TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText("New");
    
    0 讨论(0)
  • 2020-12-11 18:44

    Rather than setting the indicator as a String, use one where you set the View to be used. Then, you can hold onto that View (e.g., a TextView) and change its contents as needed.

    0 讨论(0)
  • 2020-12-11 18:48

    You should not use anymore "TabHost" for ICS, it has been deprecated in version 13. instead you need to you "ActionBar"...

    Tab mTab = super.getSupportActionBar().getTabAt(0); 
    mTab.setText("new Title");
    

    Android will refresh the tab right away

    0 讨论(0)
提交回复
热议问题