AndroidManifest中Activity声明求助:name与label参数配置疑问
name and label Parameters) Hey there! Let me walk you through this step by step—since you're new to Android Studio, figuring out manifest declarations can feel a bit overwhelming at first, but it's really simple once you know the basics.
First, the Basic Structure of an Activity Declaration
Every activity in your app needs to be registered inside the <application> tag of your AndroidManifest.xml file. Here's the core template:
<activity android:name="YOUR_ACTIVITY_CLASS_NAME" android:label="YOUR_DISPLAY_LABEL"> <!-- Optional: Add intent filters if this is a launchable activity --> </activity>
Let's Break Down the name Parameter
This is the most critical part—it tells Android exactly which Java/Kotlin class corresponds to this activity. You have two valid ways to write it:
- Relative Package Name: If your activity class is in the same base package as declared at the top of your
AndroidManifest.xml(look for thepackage="com.yourdomain.yourapp"line), you can use a dot followed by the class name. For example, if your activity is namedMainActivityand your base package iscom.example.myapp, writeandroid:name=".MainActivity". - Full Qualified Class Name: If your activity is in a subpackage (like
com.example.myapp.ui.home), you need to write the full path:android:name="com.example.myapp.ui.home.HomeActivity".
How to Get the Exact Class Name?
- Open your Activity class file (like
MainActivity.ktorMainActivity.java). At the very top, you'll see the package line (e.g.,package com.example.myapp;). Combine that with the class name (the word afterclassorpublic class) to get the full path. - Alternatively, right-click your Activity class in the Project view → Copy Reference—this will copy the full qualified name directly to your clipboard, which you can paste straight into the
nameparameter.
Understanding the label Parameter
This controls the text that shows up for your activity in places like:
- The app launcher (if this is your main activity)
- The action bar/toolbar of the activity
- The recent apps list
You have two options here too:
- Hardcoded Text: Just write a string directly, like
android:label="My Home Page". This is quick, but not recommended for apps you plan to localize (translate to other languages). - String Resource: Use a reference to a string in your
res/values/strings.xmlfile, likeandroid:label="@string/home_activity_label". This is the best practice because you can easily add translations for different languages later without changing the manifest.
Full Example
Here's a complete manifest snippet to tie it all together:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.MyApp"> <!-- Main launch activity --> <activity android:name=".MainActivity" android:label="@string/main_activity_label"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- A secondary activity in a subpackage --> <activity android:name="com.example.myapp.ui.settings.SettingsActivity" android:label="@string/settings_label" /> </application> </manifest>
Pro Tip
When you create a new Activity in Android Studio (via File → New → Activity), the IDE automatically adds the correct declaration to your manifest. Take a look at what it generates next time—this is a great way to learn by example!
内容的提问来源于stack exchange,提问作者Abby Layman




