Android naming convention

后端 未结 7 1392
眼角桃花
眼角桃花 2020-12-22 16:11

I am looking for a thorough Android naming convention suggestion. I found a little bit here:

http://source.android.com/source/code-style.html#follow-field-naming-co

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-22 16:48

    This is an excellent collection of best practices to start with: https://github.com/futurice/android-best-practices

    Here's what I use. I'll also copy from that link.

    Object naming

    • Don't use the m or s prefix as per Google guidelines. I've stopped for years and I find it easier without them. The IDE will tell you when you're using something private or static; it seems like an obsolete convention.
    • CONSTANTS start with caps
    • Acronyms should only capitalize the first letter. For example, functionUrl and unitId. Not unitID.
    • Prefix with the type of object. For example a TextView which contains a name would be tvName. An EditView with a password would be etPass.
    • If it's something usually used only once in an activity (e.g. ListView), don't be afraid to just call it lv.
    • If it's not an object type just name it by it's function. For example, if it's a string that holds the ID, name it as id, not stringId. The IDE will tell you when it's a string or a float or a long.
    • Keep it legible. Use something like Pass instead of Password.
    • Within the XML, name should be underscore with no capitals, e.g. tv_name and et_pass
    • Put the android:id as the first attribute in the XML.

    File naming

    • Prefix layouts with the type it is. E.g. fragment_contact_details.xml, view_primary_button.xml, activity_main.xml.
    • For the classes, categorize them into folders, but use suffixes. For example, /activities/MainActivity.java or /fragments/DeleteDialog.java. My folders are activities, fragments, adapters, models, and utils.
    • Adapters should say how and when they are used. So a ListView adapter for ChatActivity might be called ChatListAdapter.

    colors.xml and dimens.xml as a pallete

    • For color, use names like gray_light, not button_foreground.

    • For dimens, use names like spacing_large, not button_upper_padding.

    • If you want to set something specific for your button color or padding, use a style file.

    strings.xml

    • Name your strings with keys that resemble namespaces, and don't be afraid of repeating a value for two or more keys.

    • Use error.message.network, not network_error.

    Reasoning

    The purpose of naming conventions is not to make everything neat and consistent. It's there to flag possible mistakes and improve workflow. Most of these are designed to be convenient for keyboard shortcuts. Try to focus around minimizing bugs and improving workflow rather than looking nice.

    Prefixes are great for those, "What's the name of that TextView?" moments.

    Suffixes are there for the things which you don't access so often in that manner, but can be confusing. For example, I may not be sure whether I put my code in the Activity, Fragment, or Adapter of that page. They can be dropped if you like.

    XML ids are often in lowercase and uses underscores just because everyone seems to do it this way.

提交回复
热议问题