カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

画像ボタンを押したときに画像を入れ替える

この記事は最終更新日から1年以上経過しています。

ImageViewで画像ボタンを作成したときに、押した感覚がほしい場合に
リソースとしてXMLファイルを作成します。

・ImageViewをレイアウトに定義

<ImageView
  android:id="@+id/imageButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="right"
  android:src="@drawable/image_buttons" />

・image_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item
    android:state_pressed="true"
    android:drawable="@+drawable/image_button_on">
  </item>

  <item
    android:drawable="@+drawable/image_button">
  </item>

</selector>
この記事がお役に立ちましたらシェアお願いします
7,344 views

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です