简体   繁体   English

Android:隐藏ActionBar,保留Tabs

[英]Android: Hide ActionBar, keep Tabs

To keep this simple: I have tabs in my actionbar, but the action bar take up too much space. 为了保持这个简单:我的操作栏中有标签,但操作栏占用了太多空间。 I want that extra space. 我想要那个额外的空间。 I need a way to hide the action bar, yet keep my tabs. 我需要一种隐藏操作栏的方法,但保留我的标签。 Is there anyway to do this? 反正有没有这样做? Or is there a way I can get the tabs built into the action bar like it is in landscape mode? 或者有没有办法让我可以像操作横向模式一样将操作栏内置的选项卡? Thanks! 谢谢!

You can have an empty Actionbar, then the tabs will occupy the space: 您可以拥有一个空的Actionbar,然后选项卡将占用空间:

getSupportActionBar().setDisplayShowHomeEnabled(false);              
getSupportActionBar().setDisplayShowTitleEnabled(false);

Try the below code: 请尝试以下代码:

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);

Also remove the below in code which is added default when project is created: 同时删除创建项目时默认添加的代码中的以下内容:

public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
}

This did the trick for me 这对我有用

    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);

I also commented the line 我也评论了这条线

    getMenuInflater().inflate(R.menu.main, menu);

Ahmad's answer is correct but it requires API 11. For supporting lower APIs, use this code - Ahmad的答案是正确的但它需要API 11.为了支持更低的API,请使用此代码 -

setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

To enable it, use - 要启用它,请使用 -

setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM