【问题标题】:{Newbie} Removing ActionBar Title{Newbie} 移除 ActionBar 标题
【发布时间】:2014-06-13 17:29:09
【问题描述】:

我想从 ActionBar 中删除标题。这是我所做的:

我使用 first tutorial here 创建了一个 HelloWorld 应用程序。然后我尝试通过从 displayOptions 属性中删除 showTitle 标志来从 ActionBar 中删除标题。标题没有被删除。但是当我还设置android:label=" "时,标题被删除了,应用程序的名称也从应用程序启动器中删除。

如何仅从 ActionBar 中删除标题?

styles.xml

<resources>

   <!--
       Base application theme, dependent on API level. This theme is replaced
       by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
   -->
   <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
       <!--
           Theme customizations available in newer API levels can go in
           res/values-vXX/styles.xml, while customizations related to
           backward-compatibility can go here.
       -->
   </style>

   <!-- Application theme. -->
   <style name="AppTheme" parent="AppBaseTheme">
       <!-- All customizations that are NOT specific to a particular API-level
            can go here. -->
   </style>

   <style name="ActionBarNoTitle" parent="Widget.AppCompat.Light.ActionBar">
       <item name="displayOptions">useLogo|showHome</item>
   </style>

   <style name="NoTitleActivityTheme" parent="AppTheme">
       <item name="actionBarStyle">@style/ActionBarNoTitle</item>
   </style>

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.test.notitle"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="19" />

   <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name="com.test.notitle.MainActivity"
           android:label="@string/app_name"
           android:theme="@style/NoTitleActivityTheme" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>

【问题讨论】:

    标签: android android-activity android-actionbar android-actionbar-compat


    【解决方案1】:

    一种方法是在活动中调用getActionBar(),然后调用setDisplayShowTitleEnabled(boolean showTitle)setTitle(CharSequence title),您可以在其中传递一个空字符串。 查看ActionBar developers page了解更多详情。

    未经测试的代码:

    // Put this at the appropriate location. The `onCreate()` might be a good candidate.
    final ActionBar ab = getActionBar();
    ab.setTitle("");                      // Use this
    ab.setDisplayShowTitleEnabled(false); // OR this
    

    【讨论】:

    • 我在发布查询之前也尝试过。但是该解决方案没有提供所需的输出。当应用程序启动时,标题会在 ActionBar 中显示几秒钟,然后将其删除。这不是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    相关资源
    最近更新 更多