社区在Android的活动xml中使用ID时遵循的正确且不易出错的方法是什么


<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 代码片段具有具有属性labelFor="@+id/costPerPerson"的文本视图,其值是下面后面的 EditText 的 id。

我对天气惯例感到困惑,我应该使用现在的方式,或者应该在 EditText 中使用 @+id 并且在这里以其他方式引用它,如果是这样,它是什么?社区遵循的正确且不易出错的方法是什么?提前感谢!

如果您使用的是 IDE 及其图形布局编辑器,您可能会让它处理这些事情。

如果您更多地手动完成这项工作,则长期的指导是将+放在布局文件中自上而下的 ID 第一次出现时。在您的情况下,costPerPerson首先出现在txtCostPerPerson TextView android:labelFor,因此您的代码遵循此约定。

最新更新