Android Studio:Reformat Code格式化Xml佈局代碼後控件順序錯亂

Android Studio升級3.5之後,遇到個奇葩問題,在佈局xml文件中格式化代碼後,控件的順序都變了,這不是我們想要的結果,網上搜了一下,確實是AS3.5的鍋,每次升級都會遇到不同的問題,先記錄一下吧,以後有時間再研究爲什麼做的調整。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="sun.geoffery.mvpdemo.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="點擊按鈕獲取網絡數據" />

    <Button
        android:id="@+id/getData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取數據【成功】" />

    <Button
        android:id="@+id/getDataForFailure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取數據【失敗】" />

    <Button
        android:id="@+id/getDataForError"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取數據【異常】" />

</LinearLayout>

這個簡單的佈局界面大致如下圖所示:
未格式化的佈局位置
走你,Ctrl+Shift+F,格式化一把,再看我們的代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="sun.geoffery.mvpdemo.MainActivity">

    <Button
        android:id="@+id/getData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取數據【成功】" />

    <Button
        android:id="@+id/getDataForFailure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取數據【失敗】" />

    <Button
        android:id="@+id/getDataForError"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取數據【異常】" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="點擊按鈕獲取網絡數據" />

</LinearLayout>

原本最頂部的TextView被格式化到了最下面,一臉懵逼,界面如下:
AS3.5默認格式化後控件順序變了
具體解決方法如下:

  1. Settings–>Editor–> Code Style–>XML
  2. 點擊右上角的Set from->Predefined Style選擇Android後點擊OK保存,Perfect!
    設置配置文件
    再格式化代碼,就是我們想要的效果了
    修改配置後,格式化的佈局位置,是我們想要的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章