Hide ActionBar title just for one activity

2019-02-20 14:43发布

If I apply theme for whole application it hides ActionBar title successfully:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>

<style name="ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
    <item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>

assign in manifest:

    <application
    android:name="myapp"
    android:allowBackup="true"
    android:icon="@drawable/action_bar_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

I need ActionBar title hidden just in one activity.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
 ...other things...
</style>

<style name="MainActivityTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>

<style name="ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
    <item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>

setting this theme explicitly for one activity:

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:id="@+id/mainLayout"
   android:orientation="vertical"
   android:theme="@style/MainActivityTheme">

and it still shows title in MainActivity ActionBar. I know how to hide the title in java, I need to know what I do wrong here in XML.

3条回答
▲ chillily
2楼-- · 2019-02-20 14:58

You probably want to create a sub-theme which hides the action bar:

<style name="AppTheme.NoActionBar">
    <item name="android:windowActionBar">false</item>
</style>

and then apply it to your activity in the manifest like so:

<activity android:name="NonActionBarActivity"
          android:theme="@style/AppTheme.NoActionBar" />

Using the dot notation (AppTheme.NoActionBar) makes this NoActionBar theme inherit everything from AppTheme and override the setting on android:windowActionBar. If you prefer you can explicitly note the parent:

<style name="AppThemeWithoutActionBar" parent="AppTheme">
    <item name="android:windowActionBar">false</item>
</style>

Also, if you're using the AppCompat libraries, you probably want to add a namespace-less version to the sub-theme.

    <item name="windowActionBar">false</item>
查看更多
做自己的国王
3楼-- · 2019-02-20 15:05

If you want to remove actionbar title then just remove label attribute from <activity> tag in your manifest file.

Such as,

<activity
        android:name=".YourActivityName"
        android:label="@string/title" />

And after removing,

<activity
        android:name=".YourActivityName" />

Done!

查看更多
对你真心纯属浪费
4楼-- · 2019-02-20 15:07

Set the theme on your activity in the manifest.

<activity
    android:name=".MyActivity"
    android:theme="@style/MainActivityTheme" />
查看更多
登录 后发表回答