社区在 Android 的 Activity xml 中使用 ID 所遵循的正确且不易出错的方法是什么
What is right and less error-prone approach that the community follows for the use of IDs in Android's Activity xml
<RelativeLayout ..>
...
...
<Space
android:id="@+id/space4"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="@+id/numberOfPersons" />
<TextView
android:id="@+id/txtCostPerPerson"
android:labelFor="@+id/costPerPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/space4"
android:layout_alignBottom="@+id/costPerPerson"
android:layout_alignTop="@+id/costPerPerson"
android:gravity="center_vertical"
android:text="@string/costPerPerson"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@id/costPerPerson"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/txtCostPerPerson"
android:layout_below="@+id/space4"
android:inputType="number" />
</RelativeLayout>
以上 xml 代码片段的 TextView 具有 labelFor="@+id/costPerPerson"
属性,其值为下面紧随其后的 EditText 的 ID。
我对天气的约定感到困惑我应该使用现在的方式,还是应该在 EditText 中使用 @+id 并且在这里以其他方式引用它,如果是这样的话是什么?社区遵循的正确且不易出错的方法是什么?提前致谢!
如果您使用 IDE 及其图形布局编辑器,您可能会让它处理这些事情。
如果您更多地手工完成这项工作,长期以来的指导是将 +
放在布局文件中自上而下的 ID 的第一次出现处。在您的情况下,costPerPerson
首先出现在 txtCostPerPerson
TextView
的 android:labelFor
中,因此您的代码遵循此约定。
<RelativeLayout ..>
...
...
<Space
android:id="@+id/space4"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="@+id/numberOfPersons" />
<TextView
android:id="@+id/txtCostPerPerson"
android:labelFor="@+id/costPerPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/space4"
android:layout_alignBottom="@+id/costPerPerson"
android:layout_alignTop="@+id/costPerPerson"
android:gravity="center_vertical"
android:text="@string/costPerPerson"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@id/costPerPerson"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/txtCostPerPerson"
android:layout_below="@+id/space4"
android:inputType="number" />
</RelativeLayout>
以上 xml 代码片段的 TextView 具有 labelFor="@+id/costPerPerson"
属性,其值为下面紧随其后的 EditText 的 ID。
我对天气的约定感到困惑我应该使用现在的方式,还是应该在 EditText 中使用 @+id 并且在这里以其他方式引用它,如果是这样的话是什么?社区遵循的正确且不易出错的方法是什么?提前致谢!
如果您使用 IDE 及其图形布局编辑器,您可能会让它处理这些事情。
如果您更多地手工完成这项工作,长期以来的指导是将 +
放在布局文件中自上而下的 ID 的第一次出现处。在您的情况下,costPerPerson
首先出现在 txtCostPerPerson
TextView
的 android:labelFor
中,因此您的代码遵循此约定。