简体   繁体   中英

Can I add a comment for xml element of layout file in Android Studio?

I can add a comment for a xml layout file using <!-- --> just like Code A.

Now I hope to add a comment in xml element <androidx.recyclerview.widget.RecyclerView> , but Code B doesn't work, how can I fix it?

Code A

   <!-- This is a comment -->                          
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/mvoice_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/adView"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/layout_voice_item"
        tools:itemCount="10"
    />

Code B

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/mvoice_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/adView"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
   
    <!-- This is a comment -->    
    tools:listitem="@layout/layout_voice_item"
    tools:itemCount="10"
/>

Commenting inside a tag is not possible.

But! You can define your own namespace in parent ViewGroup:

xmlns:comment="http://com.anything.comment"

and comment any property of View with:

<androidx.recyclerview.widget.RecyclerView
    .......
    .......
    
    comment:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    comment:weight="1"
    comment:comment="Commented Text"
    tools:listitem="@layout/layout_voice_item"
    tools:itemCount="10"
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM