Eclipse の使い方 - Extract Style

Actroid Development Tools (ADT) 11.0 から、新しいリファクタリングオプションが付属しています。

この資料では新しいリファクタリングツールである Extract Style について簡単に説明します。

Eclipse ADT の新しい機能 - Extract Style

2011年6月6日にリリースされたアンドロイド開発ツール (ADT, Android Development Tools) 11.0 から、 新しいリファクタリングオプションが追加されました。

"Extract Style" (スタイルの抜き出し) というこのオプションを選択すると、 レイアウト・リソースファイルからスタイル関連属性を抜き出して、 styles.xml リソースファイルにそれらをまとめます。

Extract Style の使い方

レイアウトリソースファイルを開いている時に、"Refactor" メニューを選択します。

Extract Style の使い方

スクリーンショットのように "Extract Style" が表示されるので選択します。

すると次のダイアログが表示されます。

Extract Style の使い方

OK をクリックすると、res/values/styles.xml に次のような内容が出力されます。

Extract Style の使い方

元のレイアウトには次のように style 属性が設定されます。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
<TextView
style="@style/main"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>

尚、styles.xml の中身は次のようになりました。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="main" parent="android:Widget.TextView">
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">32sp</item>
    </style>
</resources>

ここまでお読みいただき、誠にありがとうございます。SNS 等でこの記事をシェアしていただけますと、大変励みになります。どうぞよろしくお願いします。

© 2024 Android 開発入門