简体   繁体   English

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

[英]Android : Change App Label programmatically

How can I change application label to change app name shown from java code in android?如何更改应用程序标签以更改从 android 中的 java 代码显示的应用程序名称? I'm refering to:我指的是:

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

in the Android Manifest在 Android 清单中

Is there any way to update values in strings.xml file?有什么方法可以更新strings.xml文件中的值吗?

in the activity, i tried 在活动中,我尝试了

this.setTitle("your text");

and it worked. 而且有效。 I hope it's a common solution 我希望这是一个常见的解决方案

It's not possible by the moment. 目前尚不可能。 It is a fixed string in the AndroidManifest.xml file which cannot be changed at runtime. 它是AndroidManifest.xml文件中的固定字符串,无法在运行时更改。

Using <activity-alias> you can change App icon and name to few predefined by you. 使用<activity-alias>可以将应用程序图标和名称更改为您自己预定义的名称。

Create such config in Mannifest.xml 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. 要将默认值切换为Flavor-One,请使用此代码。

 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 请记住,您必须跟踪一次仅启用一个别名

In Launcher Activity,Before setContentView() write this, 在启动器活动中,在setContentView()之前编写此代码,

setTitle("Your Title");

I don't know how it's possible, But it's surely works. 我不知道这是怎么可能的,但是肯定可以。

Application's android:label is a fixed resource referrer. 应用程序的android:label是固定资源引荐来源网址。

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 . 但是,根据提供替代资源的说法,此引荐来源网址下的字符串可以具有多个值,具体取决于配置限定符名称(values-en,-large,-land等)。

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. 如果要扩展固件,则实际上可以通过更改IconCache.java文件并使其显示带有电话某些内部值的字符串来实现。

For example if you want the SIM Toolkit to show the name of the carrier, you can do that this way. 例如,如果您想让SIM Toolkit显示运营商的名称,则可以通过这种方式进行。

But for regular apps, as it's been said, it's currently not posible. 但是对于常规应用程序,正如之前所说,目前尚不可行。

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. 但是您可以使用多个ActivityAlias ,它们可以动态启用/禁用并指向相同的targetActivity。 Therefore create your chooser for the app name - let the user select one and enable the ActivityAlias via the packageManager : 因此,为您的应用名称创建选择器-让用户选择一个并通过packageManager启用ActivityAlias

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 要隐藏旧别名,请使用带有标志的相同代码: 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. 母猪这里描述了很多方法。

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首先在 mipmap 文件夹中添加您的动态应用程序图标,然后在您的AndroidManifest.xml文件中添加以下代码

Add <activity-alias> for your app icon为您的应用图标添加<activity-alias>

<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有关更多详细信息,请参阅此github项目

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM