Android id 命名约定:带下划线的小写与驼峰式大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1832290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:41:24  来源:igfitidea点击:

Android id naming convention: lower case with underscore vs. camel case

androidmobile

提问by Juri

I'm currently programming an application for the Android. Now what I found out is that you cannot place resource objects, say, an image in the drawable folder and name it like "myTestImage.jpg". This will give you a compiler error since camel case syntax is not allowed, so you'd have to rename it like "my_test_image.jpg".

我目前正在为 Android 编写一个应用程序。现在我发现你不能在 drawable 文件夹中放置资源对象,比如图像,并将其命名为“myTestImage.jpg”。由于不允许使用驼峰式语法,这会给您一个编译器错误,因此您必须将其重命名为“my_test_image.jpg”。

But what about ids you define in the XML file. Say you have the following definition

但是您在 XML 文件中定义的 id 呢?假设你有以下定义

<TextView android:id="@+id/myTextViewFirstname"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Firstname" />

This is a valid definition, compiles and works just fine on my Android emulator although - as you see - I'm specifying the id in camel case syntax.

这是一个有效的定义,可以在我的 Android 模拟器上编译并正常工作,尽管 - 如您所见 - 我在驼峰式语法中指定了 id。

Now, the Android samples always use lower case and underscore. Is this just a naming convention to use lower case with underscore for the id's or may it cause problems on the real device?

现在,Android 示例始终使用小写和下划线。这只是使用带下划线的小写字母作为 ID 的命名约定,还是可能会在真实设备上引起问题?

Thx

谢谢

采纳答案by Dan Lew

The device will not complain if you use camel-case id names. For my first application I wrote all the ids in camel-case because I think it appears better in the Java code that way, and it works just fine.

如果您使用驼峰式 ID 名称,设备将不会抱怨。对于我的第一个应用程序,我用驼峰式写了所有的 id,因为我认为这样在 Java 代码中看起来更好,而且它工作得很好。

I am slowly changing my mind on camel-case, though, because you end up with two different naming conventions - for example:

不过,我正在慢慢改变我对驼峰式大小写的看法,因为您最终会得到两种不同的命名约定 - 例如:

// This must be undescored due to naming constrictions
setContentView(R.layout.my_long_layout_name);

// Now this looks a little out of place
findViewById(R.id.myLongSpecificId);

I, too, wonder about the standards here. Google is inconsistent in their examples; sometimes they use all lowercase, sometimes they insert underscores, and sometimes they use camel-case.

我也想知道这里的标准。谷歌在他们的例子中不一致;有时他们使用全小写,有时他们插入下划线,有时他们使用驼峰式大小写。

回答by Kiril Aleksandrov

If you take look at android.R.id.*fields, you will notice that all of them are in camel-case. So if the android ids are written in camel-case, I guess we have to follow this convention :)

如果您查看android.R.id.*字段,您会注意到它们都是驼峰式的。所以如果android ids是用驼峰写的,我想我们必须遵循这个约定:)

回答by Rogene Sagmit

i think it is good if we use the all small letters with underscores.

我认为最好使用带下划线的所有小写字母。

Just look at this(Adding to what Daniel had answered)

看看这个(加上丹尼尔的回答)

  // Camel Case
    TextView tvUserName = (TextView) findViewById(R.id.tvUserName);
  // Camel Case
    TextView tvUserName = (TextView) findViewById(R.id.tvUserName);
    // Small Caps and Underscores
    TextView tvUserName = (TextView) findViewById(R.id.tv_user_name);

in my own experience I tend to get a little confused of the camel case convention in xml because when you link it to Java which also uses camel case(because it is the standard) it looks like a doppleganger.

根据我自己的经验,我倾向于对 xml 中的驼峰大小写约定感到有些困惑,因为当您将它链接到也使用驼峰大小写的 Java 时(因为它是标准),它看起来像是一个混蛋。

回答by Yasir Ali

I think if we use underscore convention for id in xml files and camel case convention for class fields then it will give better visibility to every developer to distinguish between xml ids and class fields.

我认为如果我们在 xml 文件中对 id 使用下划线约定,对类字段使用驼峰式大小写约定,那么每个开发人员都可以更好地区分 xml id 和类字段。

回答by Micro

If you look at some of Googles app samples such as:

如果您查看一些 Google 应用示例,例如:

https://github.com/google/iosched

https://github.com/google/iosched

They use underscores. So.... maybe that is how we should be doing it?

他们使用下划线。所以......也许这就是我们应该做的?

回答by Samet ?ZTOPRAK

android:id="@+id/frag_account_button"
frag_account_button = ((ListView)view.findViewById(R.id.frag_account_button));

android:id="@+id/fragAccountButton"
fragAccountButton = ((ListView)view.findViewById(R.id.fragAccountButton));

First of all, there is no certain standard to define which one is more Proper but I have my own idea about that. My idea is reasonable to keeping XML id and java variable in the exact same name with the camel-case convention.

首先,没有确定的标准来定义哪个更合适,但我对此有自己的想法。我的想法是使用骆驼大小写约定将 XML id 和 java 变量保持在完全相同的名称中。

  1. It is easy to reach the variable by searching for the project both XML and java side.

  2. butterKnife library definition

    @BindView(R.id.infoTextView) TextViewFont infoTextView;

  1. 通过在 XML 和 java 端搜索项目,很容易找到该变量。

  2. 黄油刀库定义

    @BindView(R.id.infoTextView) TextViewFont infoTextView;

It is more proper to keep in this way.

这样保存比较合适。

回答by Paul

xml file names (which is what is used in the drawable folder) must be all lower case separated by the underscore character _ since capitalized file names are not supported in xml.

xml 文件名(这是在 drawable 文件夹中使用的)必须全部小写,并由下划线字符 _ 分隔,因为 xml 中不支持大写的文件名。

回答by Brock Woolf

If the Android's compiler is truly doing what you say restricting camel case (which seems rather odd) then you should stick to the established conventions.

如果 Android 的编译器确实按照您所说的限制骆驼大小写(这看起来很奇怪),那么您应该坚持既定的约定。

Going against the grain will only cause unnecessary confusion. Keep things consistent in all places where possible.

违背常理只会造成不必要的混乱。尽可能在所有地方保持一致。