按鈕(圖片)

除消白邊

android:background="@null"

按下後改更背景圖片

方法一、程式碼

ImageButton.setOnTouchListener(new OnTouchListener(){

@Override

public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_DOWN){

//更改為按下時的背景圖片

((ImageButton)v).setImageResource(R.drawable.login_press);

}else if(event.getAction() == MotionEvent.ACTION_UP){

//更改為抬起時的背景圖片

((ImageButton)v).setImageResource(R.drawable.login);

}

return false;

}

});

方法二、XML

建立 Color/Selector

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/button_add" android:state_pressed="false"/>

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

<item android:drawable="@drawable/button_add"/>

</selector>

在 drawable 建立 按鈕的XML檔

<ImageButton

android:id="@+id/ImageButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#00000000"

android:src="@drawable/button_add_x" >

</ImageButton>