LoginSignup
0
0

More than 1 year has passed since last update.

AndroidStudioのCodeStyle設定チートシート【XML編】

Posted at

前書き

AndroidStudioのXMLフォーマッターの設定値は多岐にわたりうんざりするので設定例を記載しておきます。

※随時更新中

設定方法

上部メニューのFile > Preferences > Code Style > XMLを開きます。
Windowsの場合はFile > Settings > Code Style > XMLです。

各XMLファイルごとの設定値は以下です。
Androidタブを開きます。

Androidタブ 該当ファイル
AndroidManifest.xml マニフェストファイル
Value Resource Files and Selectors <selector>等のリソースファイル
Layout Files レイアウトXML
Other XML resource files その他のXML

1.PNG

プロジェクト毎に設定

上部のスキーマをProjectに設定すると開いているプロジェクトのみ設定できます。
Defaultに設定するとAndroidStudioのグローバル設定になります。
2.PNG

レイアウトXML編

レイアウトXMLのデフォルトフォーマット

xmlファイルを開いてCMD + ALT + LまたはCTRL + ALT + Lでフォーマットできます。
下記はデフォルト設定でフォーマットされたソースです。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/happy_birthday_text"
        android:textColor="@color/white"
        android:textSize="36sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

最後の属性の後に改行を追加する

フォーマットをかけると末尾の>/>が改行されるようになります。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/happy_birthday_text"
        android:textColor="@color/white"
        android:textSize="36sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

設定方法はAndroidタブを開きInsert line break after last attributeにチェックをつけます。
4.PNG

名前空間宣言を含めて改行する

フォーマットをかけるとConstraintLayoutの隣にあったxmlns:android="http://schemas.android.com/apk/res/android"が改行されるようになります。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/happy_birthday_text"
        android:textColor="@color/white"
        android:textSize="36sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

設定方法はAndroidタブを開きInclude namespace declarationsにチェックをつけます。
5.PNG

名前空間宣言も含めて最後の属性の後に改行を追加する

フォーマットをかけるとConstraintLayoutの隣にあったxmlns:android="http://schemas.android.com/apk/res/android"が改行されるようになります。
また、末尾の>/>も改行されるようになります。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/happy_birthday_text"
        android:textColor="@color/white"
        android:textSize="36sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

設定方法はAndroidタブを開きInclude namespace declarationsInsert line break after last attributeにチェックをつけます。
6.PNG

AndroidManifest.xml編

※準備中

セレクターxml編

※準備中

その他XML編

※準備中

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0