android - @id 和@android :id 之间的区别

标签 android android-layout

<?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="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1" />
    <Spinner android:id="@+id/section_spinner"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_margin="5dp" />
</LinearLayout>

在这种情况下,@android:id 和@id 之间有什么区别?

最佳答案

字符串开头的 at 符号 (@) 表示 XML 解析器应该解析和扩展 ID 字符串的其余部分,并将其识别为 ID 资源。加号 (+) 表示这是必须创建并添加到我们的资源(在 R.java 文件中)的新资源名称。 Android 框架还提供了许多其他 ID 资源。引用Android资源ID时,不需要加号,但必须加上android package namespace。

@+id/section_spinner 表示您正在应用程序的命名空间中创建一个名为 section_spinner 的 ID。 您可以使用 @id/section_spinner 引用它。

@android:id/list 表示您指的是在 android 命名空间中定义的列表。

'+' 表示如果符号不存在则创建它。引用 android: 符号时不需要(也不应该使用它),因为平台已经为您定义了这些符号,无论如何您都无法在该命名空间中创建自己的符号。

关于android - @id 和@android :id 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9529871/

相关文章:

android - Kotlin:如何绕过 CancellationException

java - 如何修复使用 Firebase 的 Android 应用程序崩溃?

android - 如何强制 TableRow 到 wrap_content?

java - 如何格式化字符串以显示两位小数

android - 获取Textview中一行可以有多少个字符

android - 极限 Realm 结果

java - 如何从服务器下载 xml 布局文件并在 Android 运行时显示 View

android - 在另一个布局上设置布局

android - Cursor Adapter 的用途是什么?

android - 如何在android中将按钮对齐到正确的位置