Android Studio 布局文件格式化代码
原本布局文件的代码是下面的样子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.chenql.changeappicon.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
现在Android 建议布局文件结束标签要换行,即期望格式如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.chenql.changeappicon.MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</RelativeLayout>
进入Android Studio 设置界面,Editor -> Code Style -> XML -> Android -> Layout Files :勾选”Insert line breaks after last attribute”,即在最后一个属性后插入换行符,最后点击”Apply” 应用修改,这样Layout XML 文件在格式化代码时闭合标签就会在下一行插入了。
写在最后:
对我这种三分钟热度的人来说,坚持写blog 果然任重而道远呀!
早就想知道怎么做这样子格式化代码了,却一直懒!今天才来查看设置怎么做的!
本文介绍了如何在Android Studio中格式化布局文件代码,遵循Android建议的在布局文件结束标签后换行的规范。通过进入设置,选择Editor -> Code Style -> XML -> Android -> Layout Files,勾选'Insert line breaks after last attribute'选项,然后应用修改,即可实现布局XML文件在格式化时闭合标签在下一行的效果。
658

被折叠的 条评论
为什么被折叠?



