Android 레이아웃 XML의 주석


144

레이아웃 XML 파일에 주석을 입력하고 싶습니다. 어떻게해야합니까?

답변:


252

다른 말했듯이 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" />

3
또한 주석 내에 이중 대시를 사용할 수 없거나 XML 파서가 <!-이것으로 문제를 일으키지 만 그렇지 않습니다->
Martin Belcher-AtWrk

Eclipse를 사용하는 경우 XML 파일을 열고 주석을 원하는 위치에 커서를 놓고 최상위 메뉴 소스-> 블록 주석 추가에서 선택하십시오. 또한 "ctrl + shft + /"(즉, 제어 키와 Shift 키를 누른 다음 슬래시 키를 누릅니다). 주석 코드는 중간에 커서로 작성되므로 입력을 시작하면됩니다.
LeBeau

7
> 태그 안에는 사용할 수 없습니다. 정말 불행합니다.
linuxjava

38

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 "/>".

1
참고 : 태그 안에는 주석이 없습니다. 이 선택한 답변해야한다
Eslam 사메 아메드

1
Android Studio 팀에 강화를 제출했습니다. 데이터 바인딩을 사용하고 일부 데이터 바인딩 논리를 작성한 XML에서 한 줄의 주석을 달고 싶다면 다른 곳에 주석을 달아야하며 가시성 또는 주석이 참조하는 부분에 도움이되지 않습니다. 이것은 불가능한 일이 아니며 우리 (개발자)가 사용할 수 있도록해야합니다.
Chapz

19

XML 주석은로 시작 <!--하고로 끝납니다 -->.

예를 들면 다음과 같습니다.

<!-- This is a comment. -->

10

당신이 할 수있는 두 가지 방법이 있습니다

  1. 의견을 "<!--"시작한 다음 "-->"

    <!-- my comment goes here -->

  2. 주석을 달려는 부분을 강조 표시하고 CTRL + SHIFT + /를 누릅니다.


9

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"/>-->


4

의견 내부 태그 가능

주석 달기 / 문서 작성에 사용할 수있는 사용자 정의 속성을 작성할 수 있습니다.

아래 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 요소에 속성을 추가 하십시오. 값을 고유 한 문자열로 설정
  • 파일 내의 하위 XML 요소 아래에서 새 네임 스페이스와 다음 단어를 사용하여 컴파일 할 때 무시되는 주석 태그를 정의하십시오. 예 : <TextView my_new_namespace:my_new_doc_property="description" />

1
이러한 속성은 빌드 프로세스 중에 삭제되지 않고 대신 결과 APK 내에 저장됩니다. tools:대신 폐기되는 특수 네임 스페이스를 사용하는 것이 좋습니다. (이 답변이 게시되었을 때 존재하지 않았을 수도 있지만이 페이지는 계속 새로운 시청자를 확보합니다.)
j__m

@j__m 이것은 좋은 지적입니다. 나는 ProGuard가 이것을 자동으로 제거 할 수 있는지 또는 약간의 구성으로 제거 할 수 있는지를 보지 못했습니다 ...
CJBS

4

의견을 남기려면 다음을 Android Studio누르십시오.

Ctrl+ /Windows / Linux에서

Cmd/Mac의 경우 +

이것은 같은 strings.xml코드 파일뿐만 아니라 XML 파일에서도 작동 합니다 MainActivity.java.


3

클릭

ctrl + shift + /

그리고 당신과 evrything 의견에 쓸 것


0

한 줄에 대해 Ctrl + shift + / 및 shift + /를 눌러 주석을 추가 할 수도 있습니다.


0

믿을 수 없을 정도로 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: ....
CoolMind

-2

Federico Culloca의 메모에서 :

또한 태그 안에 사용할 수 없습니다

방법; 주석은 파일의 상단 또는 하단에 배치해야합니다. 실제로 주석을 추가하려는 모든 위치는 최소한 최상위 레벨 레이아웃 태그 내에 있습니다.


9
이것이 의미하는 것은 아닙니다. 파일 중간에 주석을 완벽하게 넣을 수 있습니다. 다른 태그 사이에 있어야합니다.
Alex Che

보다 구체적으로, 이들은 요소 n의 닫는 태그, 주석, 요소 n + 1의 여는 태그의 순서로 있어야합니다.
금지 지오 엔지니어링
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.