LoginSignup
5
4

More than 5 years have passed since last update.

Selector XMLを自動生成してくれるSelectorChapekプラグイン

Posted at

複数の状態を持つボタンなどのリソースを定義する場合、下記のようなselector xmlを作成することがよくあると思います。

button.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_normal" android:state_focused="false" android:state_pressed="false" android:state_enabled="true"/>
    <item android:drawable="@drawable/button_focused" android:state_focused="true" android:state_pressed="false" android:state_enabled="true"/>
    <item android:drawable="@drawable/button_pressed" android:state_focused="false" android:state_pressed="true" android:state_enabled="true"/>
    <item android:drawable="@drawable/button_disabled" android:state_focused="false" android:state_pressed="false" android:state_enabled="false"/>
</selector>

前から思っていましたが、面倒くさいです。
自動生成してくれるツールは無いでしょうか?
ありました。
SelectorChapek for Androidというプラグインです。

使い方

各状態に応じた画像ファイルをdrawable-xxx配下に置きます。
ファイル名の表記に揺れがあると駄目です。
たとえば無効時の画像ファイル名はxxx_disable.pngではなくxxx_disabled.pngとします。

WS000052.JPG

ツリー上のdrawableディレクトリを選択して右クリックメニュー[Generate Android Selectors]を選択します。

WS000053.JPG

button.xmlができました。
selector xmlはdrawable/配下に自動で生成されます。

WS000055.JPG

複数のアイコンを一度に追加したい場合などに非常に便利です。

ただし、この操作を行うとdrawable-xxx配下にあるすべての画像リソースを検査してxmlを生成しようとします。
それが嫌な場合は、作業用のProjectを作成して、そこで実行すると良いと思います。

リンク

SelectorChapek for Android
https://plugins.jetbrains.com/plugin/7298

5
4
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
5
4