2013-07-23 130 views
0

我有一個圖像按鈕,我已經加載了一個圖像在我的XML使用android:src標記。 我有一個基於圖像按鈕幾個問題:設置onclick圖像的圖像按鈕在Android

  1. 我的形象被調整寬高比130:51,但是當我把我的圖像按鈕的寬度和高度爲wrap_content我看到的圖像加載農作物我希望它完全適合圖像按鈕,以便間距不應該可見。

  2. 當我運行應用程序並點擊圖像按鈕時,我想要一種狀態改變的感覺。但是目前我看到它看起來像一個靜態圖像,當我點擊它時確實沒有顯示推送狀態的變化。爲了實現我想在用戶點擊事件時加載另一個圖像,以便他可以看到狀態變化並感覺到該按鈕被點擊。

請幫我一下。

回答

1

試試這個:

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

這個XML保存爲你想要的任何名稱。

然後用這個XML設置按鈕的背景。所以這會根據給定的狀態改變圖像。

+0

也請讓我知道如何設置比例因子,圖像,它應該完全貼合的ImageButton。 – user954299

+0

在圖像視圖中使用android:scaletype xml – shreyas

+0

您可以像@shreyas所說的那樣做。也可以以編程方式執行image.setScaleType(ImageView.ScaleType.MATRIX); –

0

1)my_image.setScaleType(ScaleType.FIT_XY);

2)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_pressed="true" 
      android:drawable="@drawable/btn_cab_done_pressed_example" /> 
     <item android:state_focused="true" android:state_enabled="true" 
      android:drawable="@drawable/btn_cab_done_focused_example" /> 
     <item android:state_enabled="true" 
      android:drawable="@drawable/btn_cab_done_default_example" /> 
    </selector> 
+0

感謝您的信息 – user954299