2010-07-19 43 views
0

我正在使用一些調整,允許用戶使用他們的SDCard中的資源更改我的應用程序的UI。這很簡單,沒什麼特別的,我只是使用「setImageDrawable」或「setImageBitmap」。Android:通過代碼更改按下/集中的資源

http://developer.android.com/reference/android/widget/ImageButton.html

這真的很酷,我的所有用戶都樂於定製UI。

但現在,我想也改變按鈕的按下/集中的資源!

現在,當他們點擊它時,他們看不到任何東西,他們點擊了10次。更有問題的是,這不是很直觀。

所以我想通過編程也改變壓/聚焦狀態......

阿尼想法建議嗎?

回答

5

您必須讓所有ImageButton都有一個StateListDrawable而不是BitmapDrawables。

ImageButton ib = new ImageButton(this); 
StateListDrawable sl = new StateListDrawable(); 
sl.addState(new int[] { android.R.attr.state_enabled }, yourDefaultDrawableHere); 
sl.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_selected }, yourImageDrawableHere); 
ib.setImageDrawable(sl);