레이아웃 XML 파일에 주석을 입력하고 싶습니다. 어떻게해야합니까?
레이아웃 XML 파일에 주석을 입력하고 싶습니다. 어떻게해야합니까?
답변:
다른 말했듯이 XML의 주석은 다음과 같습니다.
<!-- this is a comment -->
그것들은 여러 줄에 걸쳐있을 수 있습니다.
<!--
This is a comment
on multiple lines
-->
그러나 그들은 중첩 될 수 없습니다
<!-- This <!-- is a comment --> This is not -->
또한 태그 안에 사용할 수 없습니다
<EditText <!--This is not valid--> android:layout_width="fill_parent" />
W3C (World Wide Web Consortium)는 실제로 주석 인터페이스를 정의했습니다. 정의는 말합니다 all the characters between the starting ' <!--' and ending '-->' form a part of comment content and no lexical check is done on the content of a comment.
자세한 내용은 developer.android.com 사이트에서 확인할 수 있습니다.
따라서 시작 태그와 종료 태그 사이에 주석을 추가 할 수 있습니다. Eclipse IDE에서 간단히 입력 <!--하면 주석이 자동 완성 됩니다 . 그런 다음 사이에 주석 텍스트를 추가 할 수 있습니다.
예를 들면 다음과 같습니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".TicTacToe" >
<!-- This is a comment -->
</LinearLayout>
구체적으로 언급하는 목적은 in between태그 안에서 사용할 수 없기 때문입니다.
예를 들면 다음과 같습니다.
<TextView
android:text="@string/game_title"
<!-- This is a comment -->
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
잘못되어 다음과 같은 오류가 발생합니다
Element type "TextView" must be followed by either attribute specifications, ">" or "/>".
ctrl + shift + / 코드에 주석을 달 수 있습니다.
<!--
<View
android:layout_marginTop="@dimen/d10dp"
android:id="@+id/view1"
android:layout_below="@+id/tv_change_password"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#c0c0c0"/>-->
주석 달기 / 문서 작성에 사용할 수있는 사용자 정의 속성을 작성할 수 있습니다.
아래 documentation:info예에서 주석 값 예제와 함께 속성이 정의됩니다.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:documentation="documentation.mycompany.com"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relLayoutID"
documentation:info="This is an example comment" >
<TextView
documentation:purpose="Instructions label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to begin."
android:id="@+id/tvMyLabel"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
documentation:info="Another example comment"
documentation:translation_notes="This control should use the fewest characters possible, as space is limited"
/>
</RelativeLayout>
이 경우 documentation.mycompany.com새로운 사용자 정의 XML 네임 스페이스 (의 documentation)에 대한 정의 일 뿐이므로 고유 한 URI 문자열 입니다. 고유 한 한 아무 것도 될 수 있습니다. 는 documentation의 오른쪽으로 xmlns:도 할 수있는 일 -이는 것과 같은 방식으로 작동하는 android:XML 네임 스페이스 정의와 사용됩니다.
이 포맷을 사용하면, 임의의 수의 속성 등이 생성 될 수있다 documentation:info, documentation:translation_notes설명과 함께 값 등 어떠한 XML 속성과 같은 형식 인.
요약해서 말하자면:
xmls:my_new_namespaceXML 레이아웃 파일의 루트 (최상위 레벨) XML 요소에 속성을 추가 하십시오. 값을 고유 한 문자열로 설정<TextView my_new_namespace:my_new_doc_property="description" />tools:대신 폐기되는 특수 네임 스페이스를 사용하는 것이 좋습니다. (이 답변이 게시되었을 때 존재하지 않았을 수도 있지만이 페이지는 계속 새로운 시청자를 확보합니다.)
한 줄에 대해 Ctrl + shift + / 및 shift + /를 눌러 주석을 추가 할 수도 있습니다.
믿을 수 없을 정도로 2019 년 Android Studio 3.3 (정확한 버전은 모르겠습니다. 최소 3.3 이상)에서 이중 슬래시 주석을 XML에 사용할 수 있습니다.
그러나 XML에 이중 슬래시 주석을 사용하면 IDE에 경고가 표시됩니다.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
// this works
/* this works too */
/*
multi line comment
multi line comment
*/
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! yeah"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Unexpected text found in layout file: ....
Federico Culloca의 메모에서 :
또한 태그 안에 사용할 수 없습니다
방법; 주석은 파일의 상단 또는 하단에 배치해야합니다. 실제로 주석을 추가하려는 모든 위치는 최소한 최상위 레벨 레이아웃 태그 내에 있습니다.