Android SDK doc : http://androidappdocs.appspot.com/reference/android/widget/ImageButton.html
Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button
, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src
attribute in the<ImageButton>
XML element or by the setImageResource(int)
method.
ImageButton中修改按鈕樣式需要定一個(gè)Xml元素表,并在Layout xml中用android:src加載效果。
To remove the standard button background image, define your own background image or set the background color to be transparent.
移除標(biāo)準(zhǔn)按鈕背景圖片,自定義背景圖片或者設(shè)置背景圖片的顏色為透明。
To indicate the different button states (focused, selected, etc.), you can define a different image for each state. E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector." For example:
為了區(qū)分按鈕的不同狀態(tài)(聚焦,被選中等),你可以使用不同的圖片....
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"
- android:drawable="@drawable/button_pressed" />
- <item android:state_focused="true"
- android:drawable="@drawable/button_focused" />
- <item android:drawable="@drawable/button_normal" />
- </selector>
Save the XML file in your project res/drawable/
folder and then reference it as a drawable for the source of your ImageButton (in the android:src
attribute). Android will automatically change the image based on the state of the button and the corresponding images defined in the XML.
將該文件保存在 res/drawable/下 并使用android:src標(biāo)簽引用。 Android會(huì)自動(dòng)改變對(duì)應(yīng)狀態(tài)的背景圖片。
The order of the <item>
elements is important because they are evaluated in order. This is why the "normal" button image comes last, because it will only be applied afterandroid:state_pressed
and android:state_focused
have both evaluated false.
在<item>標(biāo)簽列表的順序是至關(guān)重要的,因?yàn)橹挥性赼ndroid:state_pressed和android:state_focuesd 同時(shí)為false的情況下,默認(rèn)的圖標(biāo)樣式才會(huì)被顯示(沒按放在那里的時(shí)候,看到的按鈕樣式)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffffff"
- >
- <Button
- android:id ="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello"
- android:textColor="#ffffffff"
- android:background="@drawable/newbtn"
- />
- </LinearLayout>