Android Studio 布局文件格式化代码

本文介绍了如何在Android Studio中格式化布局文件代码,遵循Android建议的在布局文件结束标签后换行的规范。通过进入设置,选择Editor -> Code Style -> XML -> Android -> Layout Files,勾选'Insert line breaks after last attribute'选项,然后应用修改,即可实现布局XML文件在格式化时闭合标签在下一行的效果。

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 果然任重而道远呀!
早就想知道怎么做这样子格式化代码了,却一直懒!今天才来查看设置怎么做的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值