【问题标题】:How to remove tabs in Android Activities如何删除 Android 活动中的选项卡
【发布时间】:2014-03-12 11:48:55
【问题描述】:

我在我的应用中使用标签主机作为:

当我单击任何选项卡时,我会移动到另一个活动,如下所示:

当我单击除主选项卡按钮之外的任何选项卡时,我想从屏幕上删除选项卡。这是我的代码:

public class MainActivity extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try{
    Resources ressources = getResources(); 
     TabHost tabHost = getTabHost(); 

    // Main tab
    Intent intentAndroid = new Intent().setClass(this, DasashboardActivity.class);
    TabSpec tabSpecAndroid = tabHost
        .newTabSpec("Main")
        .setIndicator("Main", ressources.getDrawable(R.drawable.ic_launcher))
        .setContent(intentAndroid);
    tabHost.clearAllTabs();


    // Log tab

    Intent intentApple = new Intent().setClass(this, LogActivity.class);
    TabSpec tabSpecApple = tabHost
        .newTabSpec("Log")
        .setIndicator("Log", ressources.getDrawable(R.drawable.ic_action_email))
        .setContent(intentApple);



    // Settings tab
    Intent intentWindows = new Intent().setClass(this, SettingsActivity.class);
    TabSpec tabSpecWindows = tabHost
        .newTabSpec("Settings")
        .setIndicator("Settings", ressources.getDrawable(R.drawable.ic_action_brightness_high))
        .setContent(intentWindows);

    // Help tab
    Intent intentBerry = new Intent().setClass(this, HelpActivity.class);
    TabSpec tabSpecBerry = tabHost
        .newTabSpec("Help")
        .setIndicator("Help", ressources.getDrawable(R.drawable.ic_action_help))
        .setContent(intentBerry);

    // add all tabs 
    tabHost.addTab(tabSpecAndroid);
    tabHost.addTab(tabSpecApple);
    tabHost.addTab(tabSpecWindows);
    tabHost.addTab(tabSpecBerry);

    //set Windows tab as default (zero based)
    tabHost.setCurrentTab(0);
    }
    catch(Exception ex)
    {
        Log.e("ERROR in Log class", ex.toString());
    }
  }


}

如何从其他已启动的活动中删除选项卡?

【问题讨论】:

  • 你想达到吗?

标签: android tabs android-tabhost removeall


【解决方案1】:

您的代码正在调用一个活动并将选项卡添加到该活动。例如:

Intent intentBerry = new Intent().setClass(this, HelpActivity.class);
TabSpec tabSpecBerry = tabHost
    .newTabSpec("Help")
    .setIndicator("Help", ressources.getDrawable(R.drawable.ic_action_help))
    .setContent(intentBerry);

使用它来仅创建 Activity,但不创建选项卡:

Intent intentBerry = new Intent().setClass(this, HelpActivity.class);
startActivity(intentBerry);

【讨论】:

  • 感谢 ElectronicGeek :)
【解决方案2】:

我不知道你想要实现什么,但是从 tabview 中删除标签肯定是不允许的,它也不好。所以我建议你禁用依赖于其他一些选项卡的选项卡的最好方法。就像用户必须在Tab B 之前通过Tab A 然后第一次禁用Tab B 并在观看Tab A 内容时启用它。

启用标签

findViewById(android.R.id.tabhost).getCurrentTabView().setEnabled(true);

禁用标签

findViewById(android.R.id.tabhost).getCurrentTabView().setEnabled(false);

【讨论】:

  • 当我点击任何标签时,即如果我点击帮助,那么帮助活动顶部不应该有标签,我不知道该怎么做:|
  • @user3381979 选项卡并不是为此而设计的,即在单击其他选项卡时消失,但是您可以禁用某些选项卡以进行某些特定验证。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多