首页 文章

如何为启动器而不是活动 Headers 设置不同的标签?

提问于
浏览
257

之前已经问过这个问题 - 但根本没有令人满意的答案!所以我再试一次 .

我想给我的应用程序启动器图标(在启动屏幕上显示的图标!)一个不同的,更短的 Headers . 似乎 Launcher 从mainfest部分获取了关于主要活动标签的标签,如下所示:

<activity android:name="MainActivity" android:label="@string/app_short_name">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

我已将此应用程序名称@ string / app_name的原始引用更改为其他更短的字符串资源 .

但是 - 大 BUT :这当然也改变了这个活动's default title! And I did not want that to happen, there'足够长的应用名称空间!使用 setTitle(int) 方法在 onCreate 中再次设置长 Headers 也没有用,因为短名称将在短时间内对用户可见,但需要足够长的时间才能注意到!

并且 - 请不要通过引用自定义 Headers 栏来回答我的问题...我不想走那么长的路,只是因为一个愚蠢的字符串 Headers !绘制一个自定义 Headers 栏以获得如此小的效果是一件痛苦的事!

no easy way 只是给启动器一个不同的字符串来显示?谢谢你的回答!

Edit: 为自定义 Headers 栏带来痛苦的另一个原因是它看起来不像默认 Headers 栏,我必须明确做些事情才能让它在每个设备上看起来都很相似!那可以想要一个不同的外观!

8 回答

  • 463

    显然 <intent-filter> 可以有一个label属性 . 如果它's absent the label is inherited from the parent component (either Activity or Application). So using this, you can set a label for the launcher icon, while still having the Activity with it'拥有自己的头衔 .

    请注意,虽然这适用于模拟器,但它可能无法在真实设备上运行,因为它取决于所使用的启动器实现 .

    http://developer.android.com/guide/topics/manifest/intent-filter-element.html

    <activity
      android:name=".ui.HomeActivity"
      android:label="@string/title_home_activity"
      android:icon="@drawable/icon">
      <intent-filter android:label="@string/app_name">
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    

    侧注:<intent-filter>也可以有一个icon属性,但莫名其妙地它不会覆盖Activity中指定的图标 . 如果您计划在SDK 11中使用本机ActionBar,它可能对您很重要,它使用Activity上指定的Icon和Logo .

    Added Info: 标签是从Activity而不是Application继承的 .

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"       
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <activity
                android:name=".StartActivity"
                android:label="@string/app_long_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    在这种情况下,如果我们没有像上面提到的那样放置标签,app_long_name将显示启动器图标 .

  • 10

    我一直在寻找同样的东西,这对我有用 .

    <activity android:name="MainActivity" android:label="@string/app_short_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    

    这将为您的应用程序启动器图标提供一个简短的名称 .

    要向应用程序栏添加更大的名称,您只需添加:

    this.setTitle(getResources().getString(R.string.app_name));
    

    到你的主要活动java文件 .

  • 0

    Mark Renouf的解决方案对我失败(使用Nexus 4和Android 4.4) . 使用快捷方式时,它会失败,快捷方式使用主要活动标签而不是应用程序名称 . 我看到像GMail和Google Keep这样的应用程序运行良好 . 但是当你打开它们时,我注意到它就像 Headers 之间的一个空白并且 Headers 出现(这似乎比使用setTitle()设置 Headers 之前闪烁的应用程序名称更好) .

    所以这是 best solution I found

    创建一个ActionBar不显示 Headers /标签的样式:

    <style name="NoActionBarTitle" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/NoActionBarTitle.ActionBar</item>
    </style>
    
    <style name="NoActionBarTitle.ActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:displayOptions">showHome|useLogo</item>
    </style>
    

    我正在使用导航抽屉并使用徽标(因为我为我的应用程序使用了徽标和图标) . 你可以使用 don't use showTitle 以外的任何东西 . 然后在AndroidManifest.xml中,为MainActivity设置主题:

    <activity
        android:name="com.xx.xxx.MainActivity"
        android:logo="@drawable/ic_icon_padding"
        android:theme="@style/NoActionBarTitle">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    然后,在MainActivity的onCreate()方法中,设置Action Bar的 Headers :

    getActionBar().setTitle(R.string.your_title);
    

    After it ,你可以致电:

    getActionBar().setDisplayShowTitleEnabled(true);
    

    棘手但值得 .

  • 2

    这可能不会满足您的要求,但是您是否考虑过创建一个非常短暂显示的启动画面(使用默认 Headers ),然后使用 setTitle(int) 方法启动您选择的新 Headers "main"活动?我没有试过这个看看它是否有效,但是这可能会创造一个令人愉快的工作,而不会表现出你想要实现的不那么无缝的性质 .

  • 29

    对于通过 setSupportActionBar() 方法使用 Support / Appcompat 工具栏的任何人,可以在工具栏XML中设置活动 Headers :

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="@string/activity_title"
        ...
    />
    

    这将覆盖清单中设置的应用程序和活动标签 .

    android.support.v7.widget.Toolbar

  • 4

    I found a workaround for this problem

    manifest.xml

    launcher(main) activityandroid:label 中写下 app's name .

    这将使您的主要活动的标签与应用标签的标签相同 .

    然后,在 Launcher(main) activityonCreate() 函数中写下此语句

    if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle("main activity label");
            }
    

    在这里写下您要为Launcher(主要)活动提供的标签 .

  • 0

    你可以这样做:

    public class FooBar extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // change title
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.main);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
        }
    }
    

    您必须创建一个自定义布局来保存 Headers . 它可以很简单(在这种情况下称为 my_title.xml ):

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="foo bar"/>
    

    在您的 AndroidManifest.xml 文件中,您只需设置应用程序的 Headers ,即将在启动器图标中显示的内容 . 对于您的活动,您无需在那里设置 Headers .

  • 1

    启动器实际上显示了声明的活动(ies)的android:label和android:icon

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    

    所以申请标签是没用的 .

相关问题