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

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

问题描述

我目前编程为Android的应用程序。现在我发现的是,你不能把资源对象,说,图像的绘制文件夹并将其命名为类似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".

但是,我们的ID,你在XML文件中定义。假设你有下面的定义

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

这是一个有效的定义,编译和工作得很好对我的Andr​​oid模拟器,虽然 - 你看 - 我指定的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?

THX

推荐答案

如果您使用驼峰ID名称的设备将不会抱怨。对于我的第一个应用程序,我写的所有的IDS在骆驼的情况下,因为我认为它会出现在c表示方式的Java $ C $好了,它工作得很好。

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天全站免登陆