没有加号的新资源 id 声明
New resource id declaration without the plus sign
这个documentation一直困扰着我:
For the ID value, you should usually use this syntax form:
"@+id/name". The plus symbol, +, indicates that this is a new resource
ID and the aapt tool will create a new resource integer in the R.java
class, if it doesn't already exist. For example:
<TextView android:id="@+id/nameTextbox"/>
我编程已经有一段时间了。但是,我从未遇到过必须使用不带加号的 ID 声明的情况。这也是违反直觉的。 ID 应该是唯一的!
有什么好的用例吗?为什么要重复使用相同的资源 ID 名称?
这意味着如果您在 layout_one.xml
中声明了一个视图,例如
<TextView
android:text="Sample Text"
android:id="@+id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
并且如果您有类似 layout_two.xml
的 textView,例如
<TextView
android:text="Sample Text2"
android:id="@+id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在这两种情况下,都只会在 R.java
中创建一个 id,并且会在另一个 xml 中重复使用(以之后调用的为准)。
所以你可以在这里生活(layout_two.xml
)
<TextView
android:text="Sample Text2"
android:id="@id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
此处 Android 将重用之前从 layout_one.xml
.
创建的 ID
您还应该阅读 Difference between "@id/" and "@+id/" in Android and What is the difference between @id and @+id?
@+id/name 当您创建 new id
"@id/" 当你 link to existing id
用例示例 1:
假设您在 XML 中创建了自己的资源:
<resources>
<item name="plusIcon" type="id"/>
</resources>
现在您可以在多个地方使用此资源,而无需使用 @+id 创建新资源。说 layout_one.xml
:
<TextView android:id="@id/plusIcon"/>
layout_two.xml
中的相同资源:
<TextView android:id="@id/plusIcon"/>
用例示例 2:
Android 框架还提供了许多其他 ID 资源。如果你想在这种情况下引用一个 Android 资源 ID,你可以使用 @android 你不需要创建你自己的新资源 ID
在 RelativeLayout 中定义约束可能是一个很好的例子。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Top"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Bottom"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/textView3"
android:layout_below="@id/textView1"
android:text="Center"/>
</RelativeLayout>
在最后一个 TextView android:layout_above 和 android:layout_below 不需要加号,因为 textView1 和 textView2 已经定义了 ID。
首先
当我们在特定的 xml 文件中第一次(按从上到下的顺序)引用一个 id 时,我们使用 +,而不是在我们创建时一些 id 第一次。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/another_button"
android:layout_alignParentTop="true"
android:text="@string/button" />
<Button
android:id="@id/another_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/another_button" />
</RelativeLayout>
其次
当有人使用 RelativeLayout 或 ConstraintLayout 时,即某些 相对父视图 他们需要多次使用相同的 id 来定义 activity 或 activity 等中的某些视图
第三次
加号 (+) 表示这是一个新的资源名称,必须创建并添加到我们的资源中(在 R.java 文件中)。
所以每次我们使用@+id/some_id,它都会触发对同一个视图的新资源引用的创建,即冗余。
示例(针对第二个用例)
<RelativeLayout
android:id="@+id/final_order_activity_order_rl"
android:layout_margin="5dp"
android:background="@drawable/gradient_for_btns"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_centerInParent="true"
android:layout_marginStart="8dp"
android:textStyle="bold"
android:textSize="18dp"
android:text="00"
android:textColor="@android:color/white"
android:maxLines="1"
android:layout_toLeftOf="@+id/final_order_activity_place_order_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/final_order_activity_total_tv" />
<Button
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:layout_marginEnd="8dp"
android:text="Place Order"
android:background="@drawable/ripple_effect"
android:textColor="@color/baseColorBright"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@id/final_order_activity_place_order_btn"/>
</RelativeLayout>
这个documentation一直困扰着我:
For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist. For example:
<TextView android:id="@+id/nameTextbox"/>
我编程已经有一段时间了。但是,我从未遇到过必须使用不带加号的 ID 声明的情况。这也是违反直觉的。 ID 应该是唯一的!
有什么好的用例吗?为什么要重复使用相同的资源 ID 名称?
这意味着如果您在 layout_one.xml
中声明了一个视图,例如
<TextView
android:text="Sample Text"
android:id="@+id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
并且如果您有类似 layout_two.xml
的 textView,例如
<TextView
android:text="Sample Text2"
android:id="@+id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在这两种情况下,都只会在 R.java
中创建一个 id,并且会在另一个 xml 中重复使用(以之后调用的为准)。
所以你可以在这里生活(layout_two.xml
)
<TextView
android:text="Sample Text2"
android:id="@id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
此处 Android 将重用之前从 layout_one.xml
.
您还应该阅读 Difference between "@id/" and "@+id/" in Android and What is the difference between @id and @+id?
@+id/name 当您创建 new id
"@id/" 当你 link to existing id
用例示例 1:
假设您在 XML 中创建了自己的资源:
<resources>
<item name="plusIcon" type="id"/>
</resources>
现在您可以在多个地方使用此资源,而无需使用 @+id 创建新资源。说 layout_one.xml
:
<TextView android:id="@id/plusIcon"/>
layout_two.xml
中的相同资源:
<TextView android:id="@id/plusIcon"/>
用例示例 2:
Android 框架还提供了许多其他 ID 资源。如果你想在这种情况下引用一个 Android 资源 ID,你可以使用 @android 你不需要创建你自己的新资源 ID
在 RelativeLayout 中定义约束可能是一个很好的例子。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Top"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Bottom"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/textView3"
android:layout_below="@id/textView1"
android:text="Center"/>
</RelativeLayout>
在最后一个 TextView android:layout_above 和 android:layout_below 不需要加号,因为 textView1 和 textView2 已经定义了 ID。
首先
当我们在特定的 xml 文件中第一次(按从上到下的顺序)引用一个 id 时,我们使用 +,而不是在我们创建时一些 id 第一次。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/another_button"
android:layout_alignParentTop="true"
android:text="@string/button" />
<Button
android:id="@id/another_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/another_button" />
</RelativeLayout>
其次
当有人使用 RelativeLayout 或 ConstraintLayout 时,即某些 相对父视图 他们需要多次使用相同的 id 来定义 activity 或 activity 等中的某些视图
第三次
加号 (+) 表示这是一个新的资源名称,必须创建并添加到我们的资源中(在 R.java 文件中)。 所以每次我们使用@+id/some_id,它都会触发对同一个视图的新资源引用的创建,即冗余。
示例(针对第二个用例)
<RelativeLayout
android:id="@+id/final_order_activity_order_rl"
android:layout_margin="5dp"
android:background="@drawable/gradient_for_btns"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_centerInParent="true"
android:layout_marginStart="8dp"
android:textStyle="bold"
android:textSize="18dp"
android:text="00"
android:textColor="@android:color/white"
android:maxLines="1"
android:layout_toLeftOf="@+id/final_order_activity_place_order_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/final_order_activity_total_tv" />
<Button
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:layout_marginEnd="8dp"
android:text="Place Order"
android:background="@drawable/ripple_effect"
android:textColor="@color/baseColorBright"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@id/final_order_activity_place_order_btn"/>
</RelativeLayout>