I want to set opacity开发者_运维百科 to my ImageButton, so when it is unselected, I can see the background a bit, and when I press on it - it becomes normal(no transparency).
If the background you are using is itself an image, then you can't simply "set" the transparency, it's coming from the png image that is the resource for the background. I'd recommend creating 3 9-patch png images for the different stages of the button using transparency as necessary for whichever stage you like. There's a description of how to use a different graphic and xml config file for your own background images in the docs on ImageButton
http://developer.android.com/reference/android/widget/ImageButton.html
If you used a solid color for the background, transparency can be achieved using a color code that has AARRGGBB as elements. android:background="#55FF0000" would be a partially transparent red background.
Use Selector (http://developer.android.com/reference/android/content/res/ColorStateList.html)
The layout code would look sth like that :
android:background="@drawable/my_selector"
and the selector code would be my_selector.xml with following content :
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/button_without_opactity" />
<item android:state_selected="true" android:drawable="@drawable/button_without_opactity" />
<item android:drawable="@drawable/button_with_opacity" />
</selector>
button_without_opacity & button_with_opacity should be 9-patches
精彩评论