クリック/プレスの色でAndroidボタンを変更するには:
色の値を定義
色の値を定義するには、プロジェクトの値のディレクトリに colors.xml ファイルを作成し、以下を追加する必要があります。res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="button_pressed">#ff8a00</color>
<color name="button_focused">#ff8a00</color>
<color name="button_default">#1c76bb</color>
</resources>
Drawable ディレクトリに XML ファイルを作成する
drawable フォルダーに button_background.xml ファイルを作成し、ボタンの押下/クリック、フォーカス、およびデフォルトの色を追加します。button_background.xml ファイルの最終的なコードは次のようになります。res/drawable/button_background.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="@color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@color/button_focused"/> <!-- focused -->
<item android:drawable="@color/button_default"/> <!-- default -->
</selector>
ボタンの追加
res/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="Click Me"
android:textColor="#fff" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp"
android:background="@drawable/button_background"
android:text="Click Me"
android:textColor="#fff" />
</RelativeLayout>
ソースはこちら。