Help us understand the problem. What is going on with this article?

【Android】ButtonやSpinnerの背景のSelectorに画像付きdrawableを使う

More than 1 year has passed since last update.

概要

ButtonやSpinnerなどの背景に画像付きdrawableを使いたいときのメモです。

結論

  • selectorのdrawableにlayer-listを使う

サンプルコード

selector

drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/default"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/pressed"
        android:state_pressed="true"/>
</selector>

押下前のdrawable

drawable/default.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@color/default" />
            <corners android:radius="@dimen/radius" />
            <stroke
                android:width="@dimen/stroke_width"
                android:color="@color/gray" />
        </shape>
    </item>
    <item android:right="@dimen/spacing_nomal" >
        <bitmap
            android:src="@drawable/ic_arrow_down"
            android:gravity="right"/>
    </item>
</layer-list>

押下後のdrawable(solidの色を変えてるだけ)

drawable/default.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@color/pressed" />
            <corners android:radius="@dimen/radius" />
            <stroke
                android:width="@dimen/stroke_width"
                android:color="@color/gray" />
        </shape>
    </item>
    <item android:right="@dimen/spacing_nomal" >
        <bitmap
            android:src="@drawable/ic_arrow_down"
            android:gravity="right"/>
    </item>
</layer-list>

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away