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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3488664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:26:30  来源:igfitidea点击:

How to set different label for launcher rather than activity title?

androidlabelandroid-manifesttitlebarlauncher

提问by Zordid

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:

我想给我的应用程序启动器图标(显示在开始屏幕上的那个!)一个不同的、更短的标题。似乎启动器从有关主要活动标签的 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>

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

我已经将我的应用程序名称 @string/app_name 的原始引用更改为不同的、更短的字符串资源。

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

但是 - 大但是:这当然也改变了这个活动的默认标题!我不希望这种情况发生,有足够的空间容纳一个很长的应用程序名称!onCreate使用该setTitle(int)方法再次设置长标题也无济于事,因为短名称对用户可见的时间很短,但足以引起注意!

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

编辑:拥有自定义标题栏的另一个原因是它看起来不像默认标题栏,我必须明确地做一些事情以使其在每个设备上看起来都一样!如果毕竟我不想要不同的外观,那也不是一个解决方案!

回答by Mark Renouf

Apparently <intent-filter>can have a label attribute. If it'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's own title.

显然<intent-filter>可以有一个标签属性。如果它不存在,则标签从父组件(活动或应用程序)继承。因此,使用它,您可以为启动器图标设置标签,同时仍然拥有带有自己标题的 Activity。

Note that, while this works on emulators, it might not work on real devices, because it depends on the launcher implementation that is used.

请注意,虽然这适用于模拟器,但它可能不适用于真实设备,因为它取决于所使用的启动器实现。

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

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>

Side Note: <intent-filter> can also have an icon attribute, but inexplicably it does notoverride the icon specified in the Activity. This may be important to you if you plan to use the native ActionBar in SDK 11+, which uses Icon and Logo specified on the Activity.

边注: <intent-filter> 也可以有一个图标属性,但是莫名其妙地不会覆盖Activity中指定的图标。如果您计划在 SDK 11+ 中使用原生 ActionBar,这对您来说可能很重要,它使用在 Activity 上指定的图标和徽标。

Added Info:The label is being inherited from Activity and not the 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>

In this case, app_long_name will be displayed with launcher icon, if we do not put label inside as mentioned above.

在这种情况下,app_long_name 将与启动器图标一起显示,如果我们不如上所述将标签放入其中。

回答by Anila

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.

到您的主要活动 java 文件。

回答by Ferran Negre

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()).

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

So here is the best solution I found:

所以这是我找到最佳解决方案

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

创建一个样式,其中 ActionBar 不显示标题/标签:

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

我正在使用导航抽屉并使用徽标(因为我为我的应用程序使用了徽标和图标)。您可以使用任何内容,但不要使用 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>

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

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

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

After it, you can call:

之后,您可以调用:

getActionBar().setDisplayShowTitleEnabled(true);

Tricky but worth.

棘手但值得。

回答by Adriaan

This probably won't satisfy what you want to do, but have you thought about creating a splash screen that displays very briefly (with the default title) and then launches your new actual "main" activity with the title of your choosing using the setTitle(int)method? I have not tried this to see if it works but that might create a pleasant work around that does not show of the less than seamless nature of what you are trying to achieve.

这可能不会满足您想要做的事情,但是您是否考虑过创建一个显示非常简短的启动画面(具有默认标题),然后使用该setTitle(int)方法以您选择的标题启动新的实际“主要”活动? 我还没有尝试过这个,看看它是否有效,但这可能会创造一个令人愉快的工作,并没有表现出你想要实现的目标的无缝性。

回答by Maksim Ivanov

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

对于通过该方法使用Support / AppcompatToolbar 的任何人setSupportActionBar(),可以在 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

android.support.v7.widget.Toolbar

回答by Tanuj Yadav

I found a workaround for this problem

In manifest.xml

Write your app's namein the android:labelof 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) activitywrite this statement

我发现了一个办法解决这个问题,

manifest.xml

写你的应用程序的名称android:label中的发射器(主)活动

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

然后,在onCreate()您的Launcher(main) 活动的函数中编写此语句

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

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

在这里写下您想要给 Launcher(main) 活动的标签。

回答by Rahul

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

启动器实际上显示了 android:label 和 android:icon 用于声明的活动

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

所以应用程序标签没有用。

回答by Cristian

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.xmlin this case):

您必须创建一个自定义布局来保存标题。它可以像(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"/>

In your AndroidManifest.xmlfile 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.

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