如何在Android Studio中重新格式化XML布局

Jak*_*ton 1 xml android-studio

在我的项目中,我在XML布局的设计选项卡中创建了一些UI 。但是当我转到“文本”选项卡时,代码格式不正确!

我从右键菜单上选择了“ 重新格式化代码”,但是代码仍然没有正确格式化!XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/nameTxt" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" android:hint="Name"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

如何重新格式化以上代码?

Ash*_*ish 12

对于重新格式化代码在 Android Studio 中正常工作

使用后您的 XML 文件代码重新格式化代码

<EditText
        android:id="@+id/nameTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)

它与CTRL + ALT + L一起使用

或者您可以单击菜单栏中的代码并选择代码格式,如图所示

在此输入图像描述


Seb*_*ski 7

Android Studio XML代码方案-如上图所示

在Android Studio偏好设置中为“默认IDE”更改XML代码方案。

希望对你有效。