【问题标题】:Hide ActionBar title just for one activity仅为一项活动隐藏 ActionBar 标题
【发布时间】:2015-02-18 01:52:07
【问题描述】:

如果我为整个应用程序应用主题,它会成功隐藏 ActionBar 标题:

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

在清单中分配

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

我需要将 ActionBar 标题隐藏在一个活动中。

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

为一项活动明确设置此主题:

<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">

它仍然在 MainActivity ActionBar 中显示标题。我知道如何在 java 中隐藏标题,我需要知道我在 XML 中做错了什么。

【问题讨论】:

标签: android android-actionbar android-styles


【解决方案1】:

在清单中为您的活动设置主题。

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

【讨论】:

    【解决方案2】:

    您可能想要创建一个隐藏操作栏的子主题:

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

    然后将其应用于清单中的活动,如下所示:

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

    使用点符号 (AppTheme.NoActionBar) 使这个 NoActionBar 主题继承 AppTheme 的所有内容并覆盖 android:windowActionBar 上的设置。如果你愿意,你可以明确地记下父级:

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

    另外,如果您正在使用 AppCompat 库,您可能希望向子主题添加一个无命名空间的版本。

        <item name="windowActionBar">false</item>
    

    【讨论】:

      【解决方案3】:

      如果您想删除actionbar title,那么只需从manifest 文件中的&lt;activity&gt; 标记中删除 label 属性。

      比如,

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

      移除后,

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

      完成!

      【讨论】:

        猜你喜欢
        • 2012-05-18
        • 2015-02-20
        • 2012-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多