Android id naming convention: lower case with underscore vs. camel case

后端 未结 8 604
北荒
北荒 2020-11-30 21:26

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 l

相关标签:
8条回答
  • 2020-11-30 22:05

    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.

    0 讨论(0)
  • 2020-11-30 22:08

    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.

    0 讨论(0)
  • 2020-11-30 22:13

    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.

    0 讨论(0)
  • 2020-11-30 22:15

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

    0 讨论(0)
  • 2020-11-30 22:17

    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.

    0 讨论(0)
  • 2020-11-30 22:18
    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.

    0 讨论(0)
提交回复
热议问题