LoginSignup
5
5

More than 5 years have passed since last update.

【Android】ボタン押下時に色を変える場合のDrawableの記述方法

Posted at

記述方法

セレクターを使って色を分ける

sample.xml
<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 通常時 -->
    <item android:state_pressed="false" android:state_focused="false">
        <shape android:shape="rectangle">
        <!-- ボタンの色 -->
        <solid android:color="#ff0000" />
        </shape>
    </item>
    <!-- フォーカス時 -->
    <item android:state_pressed="true" android:state_focused="false">
        <shape android:shape="rectangle">
        <!-- ボタンの色 -->
        <solid android:color="#00ff00" />
        </shape>
    </item>
    <!-- 押下時 -->
    <item android:state_pressed="true" android:state_focused="false">
        <shape android:shape="rectangle">
        <!-- ボタンの色 -->
        <solid android:color="#0000ff" />
        </shape>
    </item>
</selector>
5
5
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
5