在 Android 开发中,@id 与 @+id 用法的公认做法是什么?
What is the accepted practice for @id vs. @+id usage in Android development?
我是 Android 开发新手。
我注意到有时使用 @+id
而不是 @id
。据我所知,显然当你编译一个程序时,它首先扫描源数据并且 @+id
告诉它把 id 添加到 R 文件中,然后它被认为可以使用 @id
.
有什么好的做法可以用于此吗?理论上我可以总是使用 @+id
来保证安全吗?使用 @+id
一次将其添加到 R,然后删除已经存在的加号怎么样?
或者如果要在其他地方使用 @id
,是否有一种公认的做法是在何处声明 @+id
版本?
我认为一般来说,如果您将 id 分配给视图 @+id
,则引用视图 @id
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/start"
/>
@+id 在 id
第一次出现时使用。这当然意味着每当我们分配一个 id 时,都会使用 @+id
。除了这种做法,每当我们第一次引用一个 id 时,例如在 RelativeLayout
中,如果我们对特定 id 使用 above
,我们将该 id 分配给一个元素(Button
或TextView
) 在那行代码下面,我们必须使用 ...above="@+id/not_yet_assigned"
例如:
<Button
android:id="@+id/btn_first"
android:layout_above="@+id/btn_second"
android:text="ONE"
/>
<Button
android:id="@id/btn_second"
android:text="TWO"
/>
我是 Android 开发新手。
我注意到有时使用 @+id
而不是 @id
。据我所知,显然当你编译一个程序时,它首先扫描源数据并且 @+id
告诉它把 id 添加到 R 文件中,然后它被认为可以使用 @id
.
有什么好的做法可以用于此吗?理论上我可以总是使用 @+id
来保证安全吗?使用 @+id
一次将其添加到 R,然后删除已经存在的加号怎么样?
或者如果要在其他地方使用 @id
,是否有一种公认的做法是在何处声明 @+id
版本?
我认为一般来说,如果您将 id 分配给视图 @+id
,则引用视图 @id
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/start"
/>
@+id 在 id
第一次出现时使用。这当然意味着每当我们分配一个 id 时,都会使用 @+id
。除了这种做法,每当我们第一次引用一个 id 时,例如在 RelativeLayout
中,如果我们对特定 id 使用 above
,我们将该 id 分配给一个元素(Button
或TextView
) 在那行代码下面,我们必须使用 ...above="@+id/not_yet_assigned"
例如:
<Button
android:id="@+id/btn_first"
android:layout_above="@+id/btn_second"
android:text="ONE"
/>
<Button
android:id="@id/btn_second"
android:text="TWO"
/>