Android id 变数命名原则: 小写下划线与大写驼峰

我目前正在为 Android 编写一个应用程序。现在我发现,您不能将资源对象放在可绘制文件夹中,并将其命名为“ myTestImage.jpg”。由于不允许使用驼峰语法,因此这将给出一个编译器错误,所以必须将其重命名为“ my _ test _ image. jpg”。

但是您在 XML 文件中定义的 id 怎么办呢

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

这是一个有效的定义,在我的 Android 模拟器上编译和工作得很好,尽管——如您所见——我使用驼峰大小写语法指定 id。

现在,Android 示例总是使用小写和下划线。这是否只是一个在 id 中使用带下划线的小写字母的变数命名原则,还是会对真正的设备造成问题?

谢谢

49479 次浏览

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.

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

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.

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.

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);
    // 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.

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 :)

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.

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

https://github.com/google/iosched

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

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.

  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;

It is more proper to keep in this way.

If you see within Android Sample of ViewBinding layout files then you will find that ids of the views are with camel case.

So I guess it totally depends on the developer side what would be better for the team.