How Can we hide title bar while using action bar?

2020-06-07 07:31发布

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? 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.

Below is my code: (My class extends 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);

10条回答
劳资没心,怎么记你
2楼-- · 2020-06-07 07:55

Found the solution on this one. If you declare your theme on AndroidManifest.xml, the title bar will go away on its own. Although, I don't why it only works when declared on AndroidManifest.xml and not on code.

The reason for the NullPointerException is that ActionBar is synonymous to the TitleBar on higher versions (ICS and newer). By using the compatibility library and ActionBarSherlock, hiding the TitleBar will also mean hiding the ActionBar.

Below is how you can set your application to use Sherlock's theme: (You should add it on the application's tag)

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. :)

查看更多
戒情不戒烟
3楼-- · 2020-06-07 07:58

Arci's answer is not entirely correct. If you read the Theming documentation, you can find that:

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). 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.

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);
...
查看更多
兄弟一词,经得起流年.
4楼-- · 2020-06-07 07:58
    <activity android:name=".xxxxxActivity"
              android:theme="@style/Theme.Sherlock.NoActionBar"/>
查看更多
贼婆χ
5楼-- · 2020-06-07 07:59

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.

查看更多
欢心
6楼-- · 2020-06-07 08:01

see this answer to grammatically show/hide titleBar from the ActionBarSherlock ..

android: title bar hide

查看更多
混吃等死
7楼-- · 2020-06-07 08:08

If you are using Xamarin. You can use below.

ActionBar.Hide();
查看更多
登录 后发表回答