Android 在清单文件中定义活动的正确语法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11450421/
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 07:28:53  来源:igfitidea点击:

What's the correct syntax to define an activity in manifest file

androidandroid-activitysyntaxmanifest

提问by Android

What is the correct way of adding an activity to AndroidManifest.xml?

将活动添加到 的正确方法是AndroidManifest.xml什么?

Actually I have seen in many places an activity defined as

其实我在很多地方都看到过一个定义为的活动

<activity 
    android:name="MyFirstActivity" 
    android:label="@string/title_first_activity">
</activity>

and in some places as

在某些地方作为

<activity 
    android:name=".MySecondActivity" 
    android:label="@string/title_second_activity">
</activity>

I just wanted to know if putting a dot(.)is the correct way of adding an activity to the manifest file.

我只是想知道放置一个点(。)是否是将活动添加到清单文件的正确方法。

I have gone through many posts but I didn't find an exact solution. Thissuggests the dot(.) is not required, while thissuggests to use the dot(.). So what is the correct way?

我浏览了很多帖子,但没有找到确切的解决方案。表明点(.) 不是必需的,而表明使用点(.)。那么正确的做法是什么呢?

采纳答案by Ilya Demidov

dot means your package name. It's more short type of declaration.

点表示您的包名称。它是更短的声明类型。

If you define a subclass, as you almost always would for the component classes (Activity, Service, BroadcastReceiver, and ContentProvider), the subclass is declared through a name attribute. The name must include the full package designation. For example, an Service subclass might be declared as follows:

如果您定义子类,就像您几乎总是为组件类(Activity、Service、BroadcastReceiver 和 ContentProvider)所做的那样,子类将通过 name 属性声明。该名称必须包括完整的包装名称。例如,一个 Service 子类可能声明如下:

<manifest . . . >
     <application . . . >
         <service android:name="com.example.project.SecretService" . . . >
             . . .
         </service>
         . . .
     </application> 
</manifest>

However, as a shorthand, if the first character of the string is a period, the string is appended to the application's package name (as specified by the element's package attribute). The following assignment is the same as the one above:

但是,作为简写,如果字符串的第一个字符是句点,则该字符串将附加到应用程序的包名称(由元素的包属性指定)。下面的赋值与上面的相同:

<manifest package="com.example.project" . . . >
     <application . . . >
         <service android:name=".SecretService" . . . >
             . . .
         </service>
         . . .
     </application> 
</manifest> 

When starting a component, Android creates an instance of the named subclass. If a subclass isn't specified, it creates an instance of the base class.

当启动一个组件时,Android 会创建一个命名子类的实例。如果未指定子类,则会创建基类的实例。

http://developer.android.com/guide/topics/manifest/manifest-intro.htmlDeclaring class names

http://developer.android.com/guide/topics/manifest/manifest-intro.html声明类名

回答by Ishtiaq

yes putting the dot is right way.. if you see the eclipse self generated activity it looks like.

是的,加点是正确的方式.. 如果你看到 eclipse 自生成的活动,它看起来像。

 <activity 
        android:name=".MyFirstActivity" 
        android:label="@string/app_name">
    </activity>

so its the right approach, our ide can understand

所以这是正确的方法,我们的ide可以理解

回答by david m lee

Using relative paths is fine.
The path is separated by a dot, rather than a slash.

使用相对路径很好。
路径由点分隔,而不是斜线。

android:name=".one_path_level_down.MainActivity"
android:name=".one_path_level_down.DetailActivity"

The top level is the package level in your manifest file in the "package=". Something like the following:

顶层是“package=”中清单文件中的包级别。类似于以下内容:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.myapp1" >

回答by AkashG

We define package at the top under manifest tag for this purpose only that we do not have to declare it again and again if any activity resides it in the same package.We only start writing by dotto know that it belongs to the same package.All the activities residing in the same package will be accessed through this and if you declare new package other than com.example say com.example.sample than you only have to define .sample.YourActivityname.thats it.We do this refer activity from correct package.

我们在 manifest 标签下的顶部定义包只是为了这个目的,如果任何活动驻留在同一个包中,我们不必一次又一次地声明它。我们只是从开始写,知道它属于同一个包。驻留在同一包中的所有活动都将通过此访问,如果您声明 com.example 以外的新包,请说 com.example.sample 比您只需要定义 .sample.YourActivityname.thats it。我们这样做是指活动来自正确的包。

hope this will help you.

希望这会帮助你。

回答by Vinay W

http://developer.android.com/guide/topics/manifest/activity-element.html#nm

android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name

(such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the .

So given ApplicationManifest.xml:

...

then since android:name=".view.TaskListListView" has a leading period, so it is interpreted as android:name="com.stackoverflow.android.geotask.view.TaskListListView".

http://developer.android.com/guide/topics/manifest/activity-element.html#nm

android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name

(例如,“com.example.project.ExtracurricularActivity”)。但是,作为简写,如果名称的第一个字符是句点(例如,“.ExtracurricularActivity”),则会将其附加到 .

所以给定 ApplicationManifest.xml:

...

那么由于 android:name=".view.TaskListListView" 有一个前导句点,所以它被解释为 android:name="com.stackoverflow.android.geotask.view.TaskListListView"。

source

来源

回答by K_Anas

the dot is not necessary but it basically means: the activity class lives in the same package of the app. So, if your app package is: com.your.package then:

点不是必需的,但它基本上意味着:活动类位于应用程序的同一个包中。因此,如果您的应用程序包是:com.your.package,则:

  1. .YourActivitymeans that your class is inside com.your.package.
  2. YourActivitymeans that your class is inside com.your.package (same as above).
  1. .YourActivity表示你的类在 com.your.package 里面。
  2. YourActivity表示你的类在 com.your.package 里面(同上)。

So it means this is basically the same thing

所以这意味着这基本上是同一件事

To confirm my answer look here at CesarB's answer, I had also fetch the android source coe and I totally agree with him, this is how the name convention work in the AndroidManifest:

为了确认我的答案,请查看CesarB 的答案,我还获取了 android 源代码 coe,我完全同意他的观点,这就是 AndroidManifest 中名称约定的工作方式:

  1. If the name starts with a dot, always prefix it with the package.
  2. If the name has a dot anywhere else(not at the beginning), do not prefix it.
  3. If the name has no dot at all, also prefix it with the package.
  1. 如果名称以点开头,请始终使用包作为前缀。
  2. 如果名称在其他任何地方(不是开头)有一个点,请不要添加前缀。
  3. 如果名称根本没有点,还要在包前面加上前缀。

回答by RajaReddy PolamReddy

just do like this, it means you activity in this package com.your.packagewe are mentioned MySecondActivitythis activity relates to that package

就这样做,这意味着您在此包com.your.package中的MySecondActivity活动我们被提及此活动与该包有关

<activity 
     android:name="com.your.package.MySecondActivity" 
     android:label="@string/title_second_activity">
</activity>