Hide ActionBar title just for one activity

自古美人都是妖i 提交于 2019-12-04 02:09:40

问题


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.


回答1:


Set the theme on your activity in the manifest.

<activity
    android:name=".MyActivity"
    android:theme="@style/MainActivityTheme" />



回答2:


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:


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!



来源:https://stackoverflow.com/questions/28566256/hide-actionbar-title-just-for-one-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!