How to set different label for launcher rather tha

2019-01-01 14:12发布

This question has been asked before - but with no satisfying answer at all! So I'm trying it again.

I want to give my application launcher icon (the one that is displayed on the startscreen!) a different, shorter caption. It seems the launcher takes its label from the mainfest section about the main activity's label, as here:

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

I already changed the original reference to my app's name @string/app_name to a different, shorter string resource here.

BUT - big BUT: this also of course changes this activity's default title! And I did not want that to happen, there's enough space for a long application name! Setting the long title again in onCreate using the setTitle(int) method does no good either, because the short name will be visible to the user for a short time, but long enough to notice!

And - please don't answer my question by refering to a custom titlebar... I do not want to go that long way, just because of a stupid string title! It's a pain to draw a custom title bar for so little effect!

Is there no easy way to just give the launcher a different string to display? Thanks for your answers!

Edit: One more reason why having a custom titlebar is a pain is that it will not look like the default titlebar, I would have to explicitly do things to make it look alike on each device! And that can't be a solution if, after all, I don't want a different appearance!

8条回答
高级女魔头
2楼-- · 2019-01-01 14:28

You can do something like this:

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);
    }
}

You'll have to create a custom layout to hold the title. It could be as simple as (called my_title.xml in this case):

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

In your AndroidManifest.xml file you just have to set the title for the app, which is what is going to be shown in the launcher icon. For your activity you don't need to set a title there.

查看更多
爱死公子算了
3楼-- · 2019-01-01 14:28

The launcher actually shows android:label and android:icon for activity(ies) that declare

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

so application label is of no use.

查看更多
一个人的天荒地老
4楼-- · 2019-01-01 14:30

Solution of Mark Renouf fails to me (using Nexus 4 and Android 4.4). It fails when using shortcuts, shortcuts use the main activity label instead of the app name. I saw some apps like GMail and Google Keep that works fine. But when you open them, I notice its like a moment between the title is blank and the title appears (which seems better than the app name flashing before setting the title using setTitle()).

So here is the best solution I found:

Create a style where the ActionBar does not show the title/label:

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

I'm using a navigation drawer and using a logo (because I use a logo and an icon for my app). You can use whatever but don't use showTitle. Then in the AndroidManifest.xml, set the theme for the 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>

Then, in the onCreate() method of the MainActivity, set the title of your Action Bar:

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

After it, you can call:

getActionBar().setDisplayShowTitleEnabled(true);

Tricky but worth.

查看更多
墨雨无痕
5楼-- · 2019-01-01 14:36

I found a workaround for this problem

In manifest.xml

Write your app's name in the android:label of the launcher(main) activity.

This will make the label of your main activity same as that of the app label.

Then, in the onCreate() function of your Launcher(main) activity write this statement

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

Here write the label that you want to give to your Launcher(main) activity.

查看更多
查无此人
6楼-- · 2019-01-01 14:40

For anyone using Support / Appcompat Toolbar via the setSupportActionBar() method, the Activity title can be set in Toolbar XML:

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

This will override application and activity labels set in the manifest.

android.support.v7.widget.Toolbar

查看更多
弹指情弦暗扣
7楼-- · 2019-01-01 14:43

I was looking for the same thing and here's what worked for me.

<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 will give a short name to your application launcher icon.

To add a larger name to the application bar you just have to add:

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

to your main activity java file.

查看更多
登录 后发表回答