开发者

What's your naming convention for the View IDs?

I believe that many of us were bothered while naming the View IDs. Unlike the package mechanism, the IDs of the resources in a project are using one common namespace. Therefore, we have to figure out some ways to name the fields with the same functionality but in different layout files.

My way is to add the noun or the verb that the layout file's Acitvity class name used in the front of the original ID, separated by a dot. For example,an ID originally named "description" in a Activity displaying a movie's informatio开发者_JAVA百科n may become "movie.details.description".

Is there any better ideas?


First I used the same concept but now I try to use more generic ids. In you case I would simply call the id description. So you don't need an id for each element and can reuse them as it doesn't matter which layout you're referencing when you call findViewById(R.id.description) you know you get the description element of this layout.

Of course you have to know if you layout has a description element at all. This might be a disadvantage.


Interesting question. Given there is no official style guide that has been released by Google (or the community, which I may be wrong about it but I just didn't find one on Google), then it could be slightly subjective. You have to use the style that makes you and your team comfortable.

My naming convention is the following:

activityname_elementtype_explicitdescription_suffix

For instance a button Login defined inside the activity Home may become:

home_btn_login

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜