【发布时间】:2020-01-06 06:51:43
【问题描述】:
我已经在 Android 上工作了几年,并且在整个过程中,我一直在使用下划线来命名 xml 中的 id,例如blocked_numbers_title:
<TextView
android:id="@+id/blocked_numbers_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blocked Numbers"
android:gravity="center"
android:textSize="40sp"
android:textColor="@color/colorTextPrimary"
android:textStyle="bold"
tools:ignore="MissingConstraints" />
由于在 kotlin 中我们可以像变量一样直接访问这些 id,因此它们不适合使用下划线,因为它们不遵循驼峰命名法。像 blockedNumbersTitle 一样遵循 camelCase 在 XML 中命名 id 是否是一种好习惯,还是有更好的做法?
【问题讨论】:
-
是的,你应该在命名组件时使用驼峰式大小写
-
别这么认为,Android 编码标准建议使用蛇形大小写表示法(下划线表示法)
-
谢谢,如果您能提供一些官方链接,我将不胜感激。即使在 google 的所有官方示例中,他们也总是使用 _underscore,例如github.com/android/architecture-components-samples/blob/master/…
标签: android kotlin naming-conventions