如何在Xamarin Android中设置不同的应用启动名与页面标题?
Alright, let's tackle this label conflict you're dealing with—where your launcher shows "Demo App" but you want it to display "MyApp", while keeping the activity's page title as "Demo App". Here's how to fix it step by step:
What's Causing the Issue
Android uses the main Activity's Label attribute as the launcher icon's display name by default, even if you've set an app-level label in AndroidManifest.xml or AssemblyInfo.cs. That's why your launcher shows "Demo App" (from MainActivity.cs) but Gmail sees "MyApp" (from the app-level config).
The Solution: Split Label Configurations
We'll separate the launcher's app name (controlled by the app-level settings) from the activity's page title (set dynamically in code). Here's what to change:
1. Update AndroidManifest.xml
Make sure your app-level android:label (for launcher name) and android:icon are properly set—this is what the launcher will use:
<manifest> <application android:label="@string/app_name" android:icon="@drawable/AppIcon"/> </manifest>
2. Add a Dedicated String for Activity Title
In String.xml, add a new string resource to use as the activity's page title, so we don't mix it up with the app name:
<string name="app_name">MyApp</string> <string name="home_activity_title">Demo App</string>
3. Adjust MainActivity.cs
Remove the Label property from the Activity attribute—we'll set the page title via the ActionBar instead. This way, the launcher won't pick up the activity's label anymore:
[Activity(MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop)] public class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.main); // Set the ActionBar's icon and page title ActionBar.SetIcon(Resource.Drawable.Icon); ActionBar.SetTitle(Resource.String.home_activity_title); } }
Why This Works
- The launcher now reads the app-level
android:label(MyApp) as the icon's display name - We're setting the activity's page title dynamically through the ActionBar, so it shows "Demo App" once the app opens
After these changes, you'll see "MyApp" in Gmail and on the launcher, while the app's main page will have "Demo App" as its title—exactly what you wanted!
内容的提问来源于stack exchange,提问作者Anish

