Android:以编程方式更改应用程序标签

发布于 2024-12-07 02:21:21 字数 245 浏览 1 评论 0原文

如何更改应用程序标签以更改 Android 中 java 代码显示的应用程序名称? 我指的是:

<application android:icon="@drawable/icon" android:label="@string/app_name">

在 Android Manifest 中,

有没有办法更新 strings.xml 文件中的值?

How can I change application label to change app name shown from java code in android?
I'm refering to:

<application android:icon="@drawable/icon" android:label="@string/app_name">

in the Android Manifest

Is there any way to update values in strings.xml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

写给空气的情书 2024-12-14 02:21:21

在活动中,我尝试了

this.setTitle("your text");

并且有效。我希望这是一个通用的解决方案

in the activity, i tried

this.setTitle("your text");

and it worked. I hope it's a common solution

飞烟轻若梦 2024-12-14 02:21:21

暂时还不可能。它是 AndroidManifest.xml 文件中的固定字符串,无法在运行时更改。

It's not possible by the moment. It is a fixed string in the AndroidManifest.xml file which cannot be changed at runtime.

面犯桃花 2024-12-14 02:21:21

使用 您可以将应用程序图标和名称更改为您预定义的几个。

Mannifest.xml 中创建此类配置

<activity android:name="package.name.MainActivity"
 android:screenOrientation="portrait"
 android:label="@string/app_name"
 android:theme="@style/CustomTheme"
 android:launchMode="singleTask">
  <intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

<activity-alias android:label="@string/app_name_default" 
 android:icon="@drawable/icon_default" 
 android:name=".MainActivity-Default"
 android:enabled="true"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

<activity-alias android:label="@string/app_name_flavor_one" 
 android:icon="@drawable/icon_flavor_one" 
 android:name=".MainActivity-Flavor-One"
 android:enabled="false"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

现在您可以在这两个别名之间切换,因此我们将更改应用程序图标或/和名称。
要从默认切换到风味一,请使用此代码。

 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Flavor-One"), 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Default"), 
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

请记住,您必须跟踪一次仅启用一个别名

Using <activity-alias> you can change App icon and name to few predefined by you.

Create such config in Mannifest.xml

<activity android:name="package.name.MainActivity"
 android:screenOrientation="portrait"
 android:label="@string/app_name"
 android:theme="@style/CustomTheme"
 android:launchMode="singleTask">
  <intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

<activity-alias android:label="@string/app_name_default" 
 android:icon="@drawable/icon_default" 
 android:name=".MainActivity-Default"
 android:enabled="true"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

<activity-alias android:label="@string/app_name_flavor_one" 
 android:icon="@drawable/icon_flavor_one" 
 android:name=".MainActivity-Flavor-One"
 android:enabled="false"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

Now you can switch between those two aliases, therefore we will change app icon or/and name.
To switch from Default to Flavor-One use this code.

 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Flavor-One"), 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Default"), 
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Keep in mind that you have to track that only one alias will be enabled at a time

天荒地未老 2024-12-14 02:21:21

在Launcher Activity中,在setContentView()写这个之前,

setTitle("Your Title");

我不知道这是怎么可能的,但它肯定有效。

In Launcher Activity,Before setContentView() write this,

setTitle("Your Title");

I don't know how it's possible, But it's surely works.

暗喜 2024-12-14 02:21:21

是的,有可能,在这个问题中,每个人都提到这样

this.setTitle("your text");

只会更改您的活动名称而不是应用程序徽标,这里我向您展示如何动态更改应用程序徽标和应用程序名称

首先在 mipmap 中添加动态应用程序图标文件夹,然后在您的 AndroidManifest.xml 文件中添加以下代码

为您的应用程序图标添加

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <!-- Disable the original activity app icon in launcher -->
                <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>


        <activity-alias android:label="Anand"
            android:icon="@mipmap/ic_launcher"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:name=".MainActivityAlias1"
            android:enabled="true"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="Anand 1"
            android:icon="@mipmap/ic_launcher2"
            android:roundIcon="@mipmap/ic_launcher2_round"
            android:name=".MainActivityAlias2"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

    </application>

执行这些方法后在您的活动中,只需执行以下操作单击按钮

import android.content.ComponentName
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button1.setOnClickListener{
            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
            )

            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias2"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
            )
        }

        button2.setOnClickListener{
            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
            )

            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias2"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
            )
        }
    }
}

了解更多详细信息,请参阅此 github 项目

Yes, Its possible, In this question everyone mentioned like

this.setTitle("your text");

this will change only your activity name not app logo here I show you how to change the app logo and appname dynamicly

First add your dynamic app icons in the mipmap folder , after that add the below code in your AndroidManifest.xml file

Add <activity-alias> for your app icon

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <!-- Disable the original activity app icon in launcher -->
                <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>


        <activity-alias android:label="Anand"
            android:icon="@mipmap/ic_launcher"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:name=".MainActivityAlias1"
            android:enabled="true"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="Anand 1"
            android:icon="@mipmap/ic_launcher2"
            android:roundIcon="@mipmap/ic_launcher2_round"
            android:name=".MainActivityAlias2"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

    </application>

After doing these methods on your activity, just do below things on button click

import android.content.ComponentName
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button1.setOnClickListener{
            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
            )

            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias2"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
            )
        }

        button2.setOnClickListener{
            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
            )

            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias2"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
            )
        }
    }
}

for more details refer this github project

心碎的声音 2024-12-14 02:21:21

应用程序的 android:label 是固定资源引用者。

但是,根据配置限定符名称(values-en、-large、-land 等),此引荐来源网址下的字符串可能有多个值
提供替代资源

Application's android:label is a fixed resource referrer.

But the string under this referrer could have multiple values, depending on configuration qualifier names (values-en, -large, -land, etc.), according to
Providing Alternative Resources.

谁与争疯 2024-12-14 02:21:21

对于任何感兴趣的人:Android 如何更改应用程序标题
但尚不清楚它是否更改了“应用程序标签”(即应用程序列表中的图标名称)或仅更改了窗口标题。

To anyone interested: Android How to change the application title
But it's not clear if it changes the "application label" (that is, the name of the icon in the application list) or only the window title.

自控 2024-12-14 02:21:21

如果您要扩展固件,实际上可以通过更改 IconCache.java 文件并使其显示带有手机某些内部值的字符串来完成此操作。

例如,如果您希望 SIM 工具包显示运营商的名称,您可以这样做。

但正如所说,对于常规应用程序来说,目前还不可能。

If you are extending the firmware, you can actually accomplish this by changing IconCache.java file and make it show a string with some internal value of the phone.

For example if you want the SIM Toolkit to show the name of the carrier, you can do that this way.

But for regular apps, as it's been said, it's currently not posible.

记忆で 2024-12-14 02:21:21

正如史密斯先生所说,这是不可能的,

但您可以使用多个ActivityAlias,它们可以动态启用/禁用并指向相同的targetActivity。因此,为应用程序名称创建选择器 - 让用户选择一个并通过 packageManager 启用 ActivityAlias

ComponentName componentName = new ComponentName(context, context.getPackageName() + "." + aliasName);
        context.getPackageManager().setComponentEnabledSetting(componentName,
                                                               PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                                               PackageManager.DONT_KILL_APP);

要隐藏旧别名,请使用带有标志的相同代码: COMPONENT_ENABLED_STATE_DISABLED

在启用别名后,您还可以添加直接添加主启动器快捷方式的可能性。这里有很多关于母猪的方法。

As Mister Smith said, it is not possible,

but you could use multiple ActivityAlias, which can be enabled/disabled dynamically and point to the same targetActivity. Therefore create your chooser for the app name - let the user select one and enable the ActivityAlias via the packageManager:

ComponentName componentName = new ComponentName(context, context.getPackageName() + "." + aliasName);
        context.getPackageManager().setComponentEnabledSetting(componentName,
                                                               PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                                               PackageManager.DONT_KILL_APP);

To hide the old alias, use the same code with the flag : COMPONENT_ENABLED_STATE_DISABLED

You can also add the possibility to directly add a shortcut to the home launcher, after you enabled the alias. There are plenty ways described here on sow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文