将换行符添加到XML布局模板

时间:2018-03-22 01:10:06

标签: android-layout android-studio code-templates

是否可以在Android Studio中修改XML布局模板,以便命名空间和属性显示在不同的行上?

默认模板生成:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

优选:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

默认模板(下方)在单独的行中显示每个属性(不包括命名空间),但这些换行符不会转换为生成的资源文件。

Default layout resource template

1 个答案:

答案 0 :(得分:0)

问题在于Android Studio不尊重XML模板中的换行符。

解决方案

  1. 启用Keep line breaks
      

    设置&gt;编辑&gt;代码风格&gt; XML&gt;其他(标签)

  2. 在相关(参见注释)代码模板中的命名空间之前添加换行符
      

    设置&gt;编辑&gt;文件和代码模板&gt;其他(标签)

  3. 结果

    XML布局现在将在不同的行上生成命名空间和属性,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </LinearLayout>
    

    注释

    • 相关的代码模板是:

      layoutResourceFile.xml
      layoutResourceFile_vertical.xml
      
    • 确保Keep line breaks在后​​续运行时仍然存在 Android Studio,请确保修改Project计划而不是 Default计划。