How to hide a tab in Android tab layout?

后端 未结 2 1060
半阙折子戏
半阙折子戏 2020-12-11 03:13

I have a tab layout containing 3 tabs. I want to add 4th tab at run time and hide this tab after some time. Please let me know how to hide a tab in Android.

相关标签:
2条回答
  • 2020-12-11 03:54

    Well, I'm building an app that use a FragmentPagerAdapter as SectionsPagerAdapter in an activity. This activity was generated by AS and its layout has a TabLayout with some TabItems.

    I wanted to hide one of them if var hide is false. Then I used:

    Toolbar toolbar = findViewById(R.id.sectionBar);
    if(!hide) {
        TabLayout.Tab tab = tabLayout.getTabAt(2);
        if(tab!=null) {
            tabLayout.removeTab(tab);
        }
    }
    

    I know that remove is not hide but it was my solution.

    0 讨论(0)
  • 2020-12-11 04:02

    Get TabHost from resource as

    TabHost  tabHost = (TabHost)findViewById(android.R.id.tabhost);
    

    Then runtime use this

    tabHost.getTabWidget().getChildAt(3).setVisibility(View.GONE);
    

    Supposing that you are trying to hide 4th Tab.(So 3 is used)

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