Android Studio 布局文件格式化代码

时间:2022-12-19 13:17:29

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 文件在格式化代码时闭合标签就会在下一行插入了。

Android Studio 布局文件格式化代码

写在最后:
对我这种三分钟热度的人来说,坚持写blog 果然任重而道远呀!
早就想知道怎么做这样子格式化代码了,却一直懒!今天才来查看设置怎么做的!