Android id 命名约定:带下划线的小写与驼峰式 [英] Android id naming convention: lower case with underscore vs. camel case

查看:31
本文介绍了Android id 命名约定:带下划线的小写与驼峰式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

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

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

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

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

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?

谢谢

推荐答案

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

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.

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

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.

这篇关于Android id 命名约定:带下划线的小写与驼峰式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆