开发者

Show only certain tabs in android TabHost?

开发者 https://www.devze.com 2023-03-07 16:09 出处:网络
I have a TabHost with 4 tabs defined in my main.xml layout file.I am trying to setup the tabs by adding only specified tabs to the TabHost (i.e. 1, 2, and 4, or 1 and 3, or ...).

I have a TabHost with 4 tabs defined in my main.xml layout file. I am trying to setup the tabs by adding only specified tabs to the TabHost (i.e. 1, 2, and 4, or 1 and 3, or ...).

If I just add all 4 tabs, everything looks fine, but if I leave any of the 4 out, the ones I do show have no content displayed inside of them.

Does anyone know what could be the issue here?

    tabs = (TabHost) findViewById(R.id.tabhost);
    tabs.setup();

    TabSpec mainSpec = tabs.newTabSpec("Main Tab");
    mainSpec.setIndicator("Main", getResources().getDrawable(R.drawable.tab_main)).setContent(R.id.mainContent);   
    tabs.addTab(mainSpec);


    if(param1)
    {
        TabSpec msgSpec = tabs.newTabSpec("Tab 2"); 
        msgSpec.set开发者_开发问答Indicator("Messages", getResources().getDrawable(R.drawable.tab_message)).setContent(R.id.messageContent);
        tabs.addTab(msgSpec);
    }
    if(param2)
    {
        TabSpec tpSpec = tabs.newTabSpec("Tab 3"); 
        tpSpec.setIndicator("Timepoints", getResources().getDrawable(R.drawable.tab_message)).setContent(R.id.timepointContent);
        tabs.addTab(tpSpec);
    }
    if(param3)
    {
        TabSpec passSpec = tabs.newTabSpec("Tab 4"); 
        passSpec.setIndicator("Passengers", getResources().getDrawable(R.drawable.tab_message)).setContent(R.id.passengerContent);
        tabs.addTab(passSpec);
    }


Set the tab visibility programmatically and leave the content alone:

tabs.getTabWidget().getChildAt(2).setVisibility(4);

Where "2" is the id of the tab to hide and "4" is the constant for invisible.

0

精彩评论

暂无评论...
验证码 换一张
取 消