简体   繁体   English

如何在使用操作栏时隐藏标题栏?

[英]How Can we hide title bar while using action bar?

I'm using ActionBarSherlock and I'm trying to hide the title bar of my application but whenever I do that, I get a NullPointerException when accessing the ActionBar How do I remove/hide the title bar? 我正在使用ActionBarSherlock并且我试图隐藏我的应用程序的标题栏但是每当我这样做时,我在访问ActionBar时会得到NullPointerException如何删除/隐藏标题栏? Please note that I'm not referring to the title of the action bar. 请注意,我不是指操作栏的标题。 I'm referring to the title bar above the action bar. 我指的是操作栏上方的标题栏。 I'm running my application on an Android 2.3 device. 我在Android 2.3设备上运行我的应用程序。

Below is my code: (My class extends SherlockFragmentActivity .) 下面是我的代码:(我的类扩展了SherlockFragmentActivity 。)

super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Sherlock);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //I get the NullPointerException here.

//...

setContentView(R.layout.tab_navigation);

I hide the title bar by adding the following lines of code: 我通过添加以下代码行来隐藏标题栏:

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

hope this helps :) 希望这可以帮助 :)

to hide all the title bar and keep only the action bar use this code: 隐藏所有标题栏并仅保留操作栏使用此代码:

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

it worked for me 它对我有用

I was searching for an answer for this. 我正在为此寻找答案。 Found one solution. 找到一个解决方案

actionBar = getActionBar();
actionBar.hide();

The above snippet will remove the tab title and slider. 上面的代码片段将删除标签标题和滑块。 Hope it helps. 希望能帮助到你。

Found the solution on this one. 找到了这个解决方案。 If you declare your theme on AndroidManifest.xml , the title bar will go away on its own. 如果您在AndroidManifest.xml上声明主题,标题栏将自行消失。 Although, I don't why it only works when declared on AndroidManifest.xml and not on code. 虽然,我不知道它为什么只在AndroidManifest.xml上声明而不在代码上声明。

The reason for the NullPointerException is that ActionBar is synonymous to the TitleBar on higher versions (ICS and newer). NullPointerException的原因是ActionBar与更高版本(ICS和更新版本)上的TitleBar同义。 By using the compatibility library and ActionBarSherlock , hiding the TitleBar will also mean hiding the ActionBar . 通过使用兼容性库和ActionBarSherlock ,隐藏TitleBar也意味着隐藏ActionBar

Below is how you can set your application to use Sherlock's theme: (You should add it on the application's tag) 以下是如何设置应用程序以使用Sherlock的主题:(您应该将其添加到应用程序的标签上)

android:theme="@style/Theme.Sherlock"

If you know how to hide the title bar on code, please feel free to modify my answer to include your answer. 如果您知道如何在代码上隐藏标题栏,请随时修改我的答案以包含您的答案。 And if anyone can explain why it only works on the manifest file and not on code if would be nice. 如果有人能解释为什么它只适用于清单文件而不是代码,如果它会很好。 :) :)

see this answer to grammatically show/hide titleBar from the ActionBarSherlock .. 看到这个答案从ActionBarSherlock语法显示/隐藏titleBar ..

android: title bar hide android:标题栏隐藏

Arci's answer is not entirely correct. Arci的答案并不完全正确。 If you read the Theming documentation , you can find that: 如果您阅读Theming文档 ,您会发现:

The themes should be defined in your manifest for the entire application or on a per-activity basis. 主题应在整个应用程序的清单中定义,或者在每个活动的基础上定义。 You may also define the theme in the code of each activity before calling super.onCreate(Bundle) . 您还可以调用super.onCreate(Bundle) 之前在每个活动的代码中定义主题。 This must be done for every activity on which you extend from one of the 'Sherlock' activity base classes and intend to use the action bar. 必须对从“Sherlock”活动基类之一扩展的每个活动进行此操作,并打算使用操作栏。

In other words it would be enough for you to swap the first two lines of your original code: 换句话说,交换原始代码的前两行就足够了:

setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
...

I had the same problem. 我有同样的问题。 None of these answers worked for me, I still had a glimpse of the Title Bar showing right before Actionbar loaded. 这些答案都不适用于我,我仍然可以看到在Actionbar加载之前显示的标题栏。

I had to set the Activity theme to NoActionBar in the Manifest: 我必须在Manifest中将Activity主题设置为NoActionBar:

android:theme="@style/Theme.Sherlock.NoActionBar"

And then set the theme back inside my Activity right before setContentView(): 然后在setContentView()之前将主题设置回我的Activity中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTheme(R.style.Theme_Custom);
    setContentView(R.layout.activity_home);
}

This still loaded my custom theme and I no longer see a glimpse of the Title bar when activity loads 这仍然加载我的自定义主题,我不再看到活动加载时标题栏的一瞥

If you are using Xamarin. 如果您正在使用Xamarin。 You can use below. 你可以在下面使用。

ActionBar.Hide();

试试吧:

setTheme(R.style.Theme_Sherlock_NoActionBar);
    <activity android:name=".xxxxxActivity"
              android:theme="@style/Theme.Sherlock.NoActionBar"/>

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

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