Java @+id/android:list 和@+id/list 有什么区别

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

What is difference between @+id/android:list and @+id/list

javaandroidlistview

提问by Tarik

I am wondering what's the difference between @+id/android:listand @+id/list. I know the last one which is a regular id assignment but the first looks different. What makes it special?

我想知道@+id/android:list和之间有什么区别@+id/list。我知道最后一个是常规 id 分配,但第一个看起来不同。是什么让它特别?

Where I saw it:I was studying on ListView, ListAdapter and things like that and the author define the ListView in layout xml file as below :

我在哪里看到的:我正在研究 ListView、ListAdapter 和类似的东西,作者在布局 xml 文件中定义 ListView 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
<TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/main_no_items"/>
</LinearLayout>

and also let me mention @+id/android:emptyid as well.

也让我提一下@+id/android:emptyid。

And he also extends ListActivityclass.

而且他还扩展了ListActivity课程。

Here is the source of the article.

这里是文章来源

And also what's in my mind as questions are :

还有我在想的问题是:

  1. Should we extend ListActivity? Maybe I want an Activity which also contains other Views.
  2. We use @+id/android:listjust because we extend ListActivityor we can use the same convention if we extend Activity?
  1. 我们应该延长ListActivity吗?也许我想要一个还包含其他视图的活动。
  2. 我们使用@+id/android:list只是因为我们扩展,ListActivity或者如果我们扩展,我们可以使用相同的约定Activity

Thanks.

谢谢。

采纳答案by EboMike

Resource IDs in Android are specific to a package (which is good, or else you'd have lots of conflicts if your app is dealing with several packages at the same time).

Android 中的资源 ID 特定于一个包(这很好,否则如果您的应用程序同时处理多个包,您就会有很多冲突)。

@+id/listwill create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.

@+id/list将在您的应用程序(=您的包)中创建一个名为“list”的资源 ID,并为其提供唯一 ID。在代码中,这将是R.id.list.

@android:id/listwill use the ID "list" from the package android (which, in code, would be android.R.id.list.

@android:id/list将使用包 android 中的 ID“list”(在代码中,它将是android.R.id.list.

EDIT: Need to add the corrections David Hedlund pointed out: The proper reference would be @android:id/list. Also, +indicates you're defining a new ID - you obviously don't need that when you're referencing something that was defined in the Android API.

编辑:需要添加 David Hedlund 指出的更正:正确的参考是@android:id/list. 此外,+表示您正在定义一个新 ID - 当您引用在 Android API 中定义的某些内容时,您显然不需要它。

回答by Eric Levine

I think the example code you posted has a typo, so it should be @android:id/list(without the +). From the ListActivity javadoc:

我认为您发布的示例代码有错别字,所以应该是@android:id/list(没有 +)。从ListActivity javadoc

your own view MUST contain a ListView object with the id "@android:id/list"

您自己的视图必须包含一个 ID 为“@android:id/list”的 ListView 对象

@android:id/listis specific to ListActivity, so you do not need it if you are adding a ListView into any other kind of Activity. You should extend ListActivity if you want the user to do more than view the list. For example, you can override ListActivity.onListItemClickto respond to clicks on an item in the list.

@android:id/list特定于 ListActivity,因此如果您将 ListView 添加到任何其他类型的活动中,则不需要它。如果您希望用户做的不仅仅是查看列表,您应该扩展 ListActivity。例如,您可以覆盖ListActivity.onListItemClick以响应对列表中项目的点击。

Similarly, @id/android:empty(again, without the +), is a special case for ListActivity. This allows you to specify an alternative view that should be displayed when your list is empty. That View will not be displayed when the list is populated.

同样,@id/android:empty(再次,没有 +),是 ListActivity 的一个特例。这允许您指定当列表为空时应显示的替代视图。填充列表时不会显示该视图。

回答by Ahmed Ali

in android,

在安卓中,

In XML: @[package:]layout/filename

在 XML 中:@[package:]layout/filename

like

喜欢

android:id="@+id/android:list"

This is the standard way to refer to a list view when refering to listFragment or listActivity

这是在引用 listFragment 或 listActivity 时引用列表视图的标准方法

so filename is android:list is a reference to ListView.

所以文件名是 android:list 是对 ListView 的引用。

navigate to res/values/ids.xml

导航 res/values/ids.xml

you will find <item type="id" name="list" />

你会找到 <item type="id" name="list" />

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

ListView 是一个显示可滚动项目列表的视图组。使用适配器将列表项自动插入到列表中,该适配器从源(例如数组或数据库查询)中提取内容,并将每个项结果转换为放置到列表中的视图。