【问题标题】:camelCase for ids in xml while using Kotlin in Android在 Android 中使用 Kotlin 时用于 xml 中的 ids 的 camelCase
【发布时间】: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 kotlin naming-conventions


【解决方案1】:

您可以使用 CamelCase 或任何其他格式的 id 命名约定,只要您对它感到满意,因为它是一个字符串变量。

请参阅文档以获取更多信息:

android kotlin official doc

【讨论】:

    【解决方案2】:

    camelCase 是一个不错的选择,因为它具有良好的可读性 - 您可以区分 - 以及它的紧凑性。关于可读性,有时很难识别变量名中何时包含多个单字母单词:textToDoAWorkAWork park 看起来有点奇怪,但总的来说,我认为这是最好的选择

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 2018-12-27
      • 2018-10-13
      • 2020-03-23
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多