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에 밑줄과 함께 소문자를 사용하는 명명 규칙입니까, 아니면 실제 장치에서 문제를 일으킬 수 있습니까?
고마워
낙타 케이스 ID 이름을 사용하면 장치가 불평하지 않습니다. 내 첫 번째 응용 프로그램의 경우 모든 ID를 낙타 케이스로 작성했습니다. Java 코드에서 그런 방식으로 더 잘 보이고 잘 작동한다고 생각하기 때문입니다.
하지만 낙타 케이스에 대해서는 천천히 마음을 바꾸고 있습니다. 두 가지 다른 이름 지정 규칙을 사용하기 때문입니다. 예를 들면 다음과 같습니다.
// 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);
나도 여기의 표준에 대해 궁금합니다. Google은 그들의 예에서 일관성이 없습니다. 때로는 모두 소문자를 사용하고 때로는 밑줄을 삽입하고 때로는 낙타 대문자를 사용합니다.
android.R.id.*
필드 를 살펴보면 모두 낙타 케이스에 있음을 알 수 있습니다. 따라서 안드로이드 ID가 낙타 케이스로 작성되면이 규칙을 따라야한다고 생각합니다. :)
나는 그가 xml
파일 내부의 ID에 대해 이야기하고 있다고 생각 합니다.
예 :
android:id="@+id/home_button"
대
android:id="@+id/HomeButton"
이 문제에 대한 규칙이나 지침을 찾지 못했기 때문에 내 프로젝트의 개발자가 두 가지 방법을 모두 사용하고 있습니다.
밑줄이있는 소문자를 모두 사용하면 좋을 것 같아요.
이것 좀 봐 (다니엘이 대답 한 것에 더해)
// Camel Case TextView tvUserName = (TextView) findViewById(R.id.tvUserName);
// Small Caps and Underscores
TextView tvUserName = (TextView) findViewById(R.id.tv_user_name);
내 경험상 xml의 낙타 케이스 규칙을 약간 혼동하는 경향이 있습니다. 왜냐하면 낙타 케이스 (표준이기 때문에)를 사용하는 Java에 연결할 때 도플 갱어처럼 보이기 때문입니다.
다음과 같은 Google 앱 샘플을 살펴보면 다음과 같습니다.
https://github.com/google/iosched
밑줄을 사용합니다. 그래서 .... 우리가 그렇게해야할까요?
xml 파일의 id에 대한 밑줄 규칙과 클래스 필드에 대한 camel case 규칙을 사용하면 모든 개발자가 xml ID와 클래스 필드를 구별하기 위해 더 나은 가시성을 제공 할 것이라고 생각합니다.
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 a certain standard to define which one is more valid but I have a few reasons to prove that. My idea is reasonable to keeping XML id and java variable in the exact same name with camel-case convention.
It is easy to reach the variable by searching for the project both XML and java side.
butterKnife library definition
@BindView(R.id.infoTextView) TextViewFont infoTextView;
It is more proper to keep in in this way.
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.
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.
'developer tip' 카테고리의 다른 글
R system.time (exp) 출력에서 측정하는 '사용자'및 '시스템'시간은 무엇입니까? (0) | 2020.09.19 |
---|---|
색상 선택기 (점 안기)를 사용하는 방법은 무엇입니까? (0) | 2020.09.19 |
WPF 앱과 비즈니스 앱용 Winform의 장점은 무엇입니까? (0) | 2020.09.19 |
구현하기 가장 쉬운 보로 노이 다이어그램 알고리즘? (0) | 2020.09.19 |
Visual Studio, 솔루션 별 들여 쓰기 설정 (0) | 2020.09.19 |